GreenSaguaro
GreenSaguaro

Reputation: 3377

RabbitMQ - How to Dead-letter / Process Messages in Expired Queues?

I have an a queue that has x-expires set. The issue I am having is that I need to do further processing on the messages that are in the queue IF the queue expires. My initial idea was to set x-dead-letter-exchange on the queue. But, when the queue expires, the messages just vanish without making it to the dead-letter exchange.

How can I dead-letter, or otherwise process, messages that are in a queue that expires?

Upvotes: 2

Views: 1655

Answers (2)

Bogdan Minciu
Bogdan Minciu

Reputation: 438

As suggested in the comments, you cannot do this by relying only on the x-expire feature. But a solution that worked in a similar case I had was to:

  1. Use x-message-ttl to make sure messages die if not consumed in a timely manner,
  2. Assign a dead letter exchange to the queue where all those messages will be routed,
  3. Use x-expires to set the queue expiration to a value higher than the TTL of the messages,
  4. (and this is the tricky part) Assuming you have control over your consumers, before the last consumer goes offline, delete the binding to your "dying" queue, potentially through a REST API call - this will prevent new messages from being routed to the queue.

This way the messages that were published before the last consumer died were already processed, existing messages will be dead-lettered before the queue expires, and new messages cannot come into the queue.

Upvotes: 1

Paolo Costa
Paolo Costa

Reputation: 1989

You need to add a new dead letter queue that is bound to your dead letter exchange with the binding routing key set as the original queue name. In this way all expired messages sent to the dead letter exchange are routed to the dead letter queue.

Upvotes: 0

Related Questions