Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best strategy to handle exception in spring amqp SimpleMessageListenerContainer

I have two confusions.

1.If RuntimeException thrown from message listener, does SimpleMessageListenrContainer stop? 2.If SimpleMessageListenerContainer not stop, what is behavior about the auto acknowledgement?

Currently, I want that if the message listener handle message failed, i just log the error and don't stop the container meanwhile notify the broker have received message.

Now I just catch the throwable in message listener method, is it the right way?

like image 736
GrapeBaBa Avatar asked Oct 17 '25 23:10

GrapeBaBa


1 Answers

No, the container won't stop.

If the listener throws an exception, the message is rejected (and requeued by default). You can change the default behavior to discard the message by setting defaultRequeueRejected to false (it's true by default). Or throw an AmqpRejectAndDontRequeueException which instructs the container to reject (and not requeue) the message - the ack is sent as if the listener had thrown no exception.

When a message is rejected without requeuing, it is either discarded or sent to the dead letter exchange, if the queue is so configured.

like image 159
Gary Russell Avatar answered Oct 19 '25 18:10

Gary Russell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!