Sending acknowledgment to rabbitmq server in depen

2020-05-06 10:07发布

问题:

First of all, I present context of my case:
I am using spring-boot and spring-rabbitmq. It is working for me, you should know that I must have implemented custom converter for received messages.
(1) From this converter can be thrown exception, for instance in case of inproper message and so on.
(2) After successfull converting (no exception) listener is invoked. Then, in the listener it can also be thrown exception.


Now, I would like to force two things:
(1') Don't requeue message in case of expcetion in converter. Simply, send acknowledgment to queue and simulate that everything is alright.
(2') What is default setting in this case ? When internal spring-rabbitmq engine decide to send acknowledgment to queue ? When it decide command to requeue ? Is it possible to manage it in depends on situation ?

I found in docs:

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.

For example, in depends on catched exception in listener I should decide if I would like to requeue message, as I think (simply by throwing from catch AmqpRejectAndDontRequeueException). I am not sure if it is good way and it is why I am asking you about your opinion.

回答1:

Please read the reference manual.

The behavior is (mostly) controlled by the ErrorHandler.

Throw a MessageConversionException - the container requeues messages for most exceptions but some exceptions are considered fatal. Generally, if a message can't be converted, then redelivering it makes no sense.

This is all clearly explained in the section (surprisingly?) called 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.

You can customize the ErrorHandler as needed.