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

I saw blog posts as well as answer # 2 from this SO question. How to set up multiple retry attempts in RabbitMQ? , recommending this one way to keep track of the number of retries in the RabbitMQ message, you should publish it again with the x-redelivered-count header.

This is done by setting the message header when it is sent to the exchange of dead letters, or this is done by creating a new copy of the message with the same headers and body as before, but plus the added x-redelivered-count (while ACKing is an old copy messages?), and if this can be done with the first, how do I edit the headers or body of the message before it is dead?

+5
source share
1 answer

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

This thread indicates that they are working on how the server automatically reports the number of delivery attempts, but this does not happen in the current version of RabbitMQ.

So ... at present, if it is important for you to know how many times your code has tried to process the message, you will need to:

  • Reject the message by setting requeue=false and
  • Publish the message again using the original exchange and routing key, but adding any information needed for the header. From a server perspective, this is a completely new message, and it will be placed at the back of the queue.

Please note that messages can still be requested if your consumer disconnects before sending any confirmation, positive or negative, back to the server. In this case, the original message will be delivered as is (without a custom header), and the redelivered flag will be set.

+2
source

Source: https://habr.com/ru/post/1275025/


All Articles