向 rabbitmq 服务器发送确认取决于转换器和侦听器

Sending acknowledgment to rabbitmq server in depends on converter and listener

首先,我介绍一下我的案例背景:
我正在使用 spring-bootspring-rabbitmq。它对我有用,你应该知道我必须为收到的消息实现自定义转换器。
(1) 从这个转换器可以抛出异常,例如在不正确的消息等情况下。
(2) 成功转换(无异常)后调用侦听器。然后,在侦听器中它也可以抛出异常。


现在,我想强制执行两件事:
(1') 在转换器发生异常时不要重新排队消息。简单地说,向队列发送确认并模拟一切正常。
(2') 这种情况下的默认设置是什么?当内部 spring-rabbitmq engine 决定向队列发送确认时?什么时候决定命令重新排队?是否可以根据情况进行管理?

我在文档中找到:

If retries are not enabled and the listener throws an exception, by default the delivery will be retried indefinitely. You can modify this behavior in two ways; set the defaultRequeueRejected property to false and zero re-deliveries will be attempted; or, throw an AmqpRejectAndDontRequeueException to signal the message should be rejected. This is the mechanism used when retries are enabled and the maximum delivery attempts are reached.

例如,取决于侦听器中捕获的异常我应该决定是否要重新排队消息,就像我想的那样(只需从 catch AmqpRejectAndDontRequeueException 中抛出)。我不确定这是否是个好方法,这就是为什么我要问你的意见。

请阅读reference manual

该行为(大部分)由 ErrorHandler 控制。

抛出 MessageConversionException - 容器为大多数异常重新排队消息,但一些异常被认为是致命的。一般来说,如果一条消息无法转换,那么重新发送它就没有意义了。

这一切都在名为 Exception Handling

的部分(令人惊讶?)中得到了清楚的解释

Starting with version 1.3.2, the default ErrorHandler is now a ConditionalRejectingErrorHandler which will reject (and not requeue) messages that fail with an irrecoverable error:

o.s.amqp...MessageConversionException

o.s.messaging...MessageConversionException

o.s.messaging...MethodArgumentNotValidException

o.s.messaging...MethodArgumentTypeMismatchException

java.lang.NoSuchMethodException

java.lang.ClassCastException

The first can be thrown when converting the incoming message payload using a MessageConverter. The second may be thrown by the conversion service if additional conversion is required when mapping to a @RabbitListener method. The third may be thrown if validation (e.g. @Valid) is used in the listener and the validation fails. The fourth may be thrown if the inbound message was converted to a type that is not correct for the target method. For example, the parameter is declared as Message but Message is received.

The fifth and sixth were added in version 1.6.3.

您可以根据需要自定义 ErrorHandler