使用 Spring AMQP 将消息连同错误代码一起发布到 DLX

Publish message to DLX along with error codes with Spring AMQP

当由于信息不足或通过 queue 通过 Spring AMPQ 为 queue 绑定 DLX 收到的消息出现任何其他问题而无法处理时,我可以将消息发布到 DLX。

例如,收到的发票缺少计费时间and/or没有员工 ID 显示它。

{
  "invoiceId": "INV1234",
  "hourRate": 18.5,
  "hoursWorked": 0,
  "employeeId": "EMP9900"
}

由于请求的大小较小 body,很容易理解问题所在。但是,我们有一些相当大的要求 body 长度和 15-20 个验证点。

消息的生产者期望在将消息推回以通过 DLX 发布回消息时出现什么问题。

针对这个需求,我有以下两个想法。

选项 #1:将错误信息附加到原始邮件。

{
  "message": {
    "invoiceId": "INV1234",
    "hourRate": 18.5,
    "hoursWorked": 0,
    "employeeId": "EMP9900"
  },
  "errors": [
    {
      "errorCode": "001",
      "errorMessage": "Invalid no. of hours."
    },
    {
      "errorCode": "002",
      "errorMessage": "Employee not found in the system."
    }
  ]
}

选项 #2:在 headers

中添加其他错误 object

在这两个选项中,

  1. 处理此要求的更好方法是什么?并且
  2. spring-amqp 或任何其他库中是否有可用的 in-built 解决方案?

参见 the documentation。该框架实现了您的 #2 解决方案。

Starting with version 1.3, a new RepublishMessageRecoverer is provided, to allow publishing of failed messages after retries are exhausted.

...

The RepublishMessageRecoverer publishes the message with additional information in message headers, such as the exception message, stack trace, original exchange, and routing key. Additional headers can be added by creating a subclass and overriding additionalHeaders().