Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the headers and body of a RabbitMQ message when retrying it?

Tags:

rabbitmq

I've seen blog posts, as well as answer #2 from this SO question How do I set a number of retry attempts in RabbitMQ? recommending that one way to keep track of the retry count on a RabbitMQ message is to publish it again with an x-redelivered-count header.

Is this done by setting the header on a message as it's headed to the dead-letter exchange, or is it done by making a fresh copy of the message with the same headers and body as before, but plus an incremented x-redelivered-count (while ACKing the old copy of the message?), and if it can be done with the former, how would I edit the headers or body of a message before it is dead-lettered?

like image 477
Andy Haskell Avatar asked Jan 29 '18 19:01

Andy Haskell


1 Answers

When you do a basic.nack in RabbitMQ, what you are telling the server is that the message cannot be processed. If you have a dead letter exchange/queue configured, the message will be routed there if you set requeue=false. You cannot alter the contents of a message published to the DLX, as they are only a reflection of the original message that was published.

This thread indicates that they are working on a way for the server to automatically report the number of delivery attempts, but it does not happen in the current version of RabbitMQ.

So... presently, if it is important for you to know the number of times that your code attempted to process the message, it will be necessary to:

  1. Reject the message, setting requeue=false and
  2. Publish the message again, using the original exchange and routing key, but adding whatever information you need to the header. From the perspective of the server, this is a brand-new message, and it will be placed at the back of the queue.

Note that messages still may be requeued in the case that your consumer disconnects before sending any acknowledgement, positve or negative, back to the server. In this case, the original message will be delivered as-is (no custom header) and the redelivered flag will be set.

like image 192
theMayer Avatar answered Sep 28 '22 01:09

theMayer