Wednesday, March 30, 2016

RabbitMQ DeadLetter Queue with a Requeueing Threshold

When an exception is thrown on a Spring consumer for RabbitMQ, Spring's default behavior is to requeue the message on the head of the queue. This usually results in a message being infinitely requeued if the exception is thrown again. Not only does this cause an infinite loop in requeueing, since the message is placed at the head of the queue, all other messages are stopped from being processed if there is only one consumer.

Some solutions are to:

1. Wrap the Exception with a AmqpRejectAndDontRequeueException.

2. Change the default to not requeue. If you change the default to not requeue, then you need to setup a deadletter exchange and queue otherwise the message is dropped on the floor.

3. A more sophisticated solution is to setup a retry threshold and then deadletter the message.
Example: