英文:
RabbitMQ: How to requeue message with counter
问题
有没有办法在不手动重新排队作业的情况下计算作业被重新排队(通过拒绝或Nak)的次数?
我需要在尝试了'n'次后放弃作业。
附注:目前我手动重新排队作业(删除旧作业,创建一个具有相同内容和额外计数器标头的新作业,如果计数器不存在或值小于'n')。
英文:
Is there any way to count how many time a job is requeued (via Reject or Nak) without manually requeu the job?
I need to retry a job for 'n' time and then drop it after 'n' time.
ps : Currently I requeue a job manually (drop old job, create a new job with the exact content and an extra Counter header if the Counter is not there or the value is less than 'n')
答案1
得分: 24
基于quorum队列的2023年更新的毒消息处理方式:
> Quorum队列跟踪未成功传递尝试的次数,并在任何重新传递的消息中使用“x-delivery-count”头部进行公开。
在添加队列和流队列之前的原始答案:
当消息重新传递一次或多次时,会设置redelivered
消息属性为true。
如果您想要跟踪重新传递次数或剩余重新传递次数(也称为IP堆栈中的跳数限制或TTL),您需要将该值存储在消息体或头部中(字面上,消费消息,修改它,然后将修改后的消息重新发布到代理)。
还有一个类似的问题和答案可能会对您有帮助:如何在RabbitMQ中设置重试次数?
英文:
Update from 2023 based on quorum queue's way of poison message handling:
> Quorum queues keep track of the number of unsuccessful delivery
attempts and expose it in the "x-delivery-count" header that is included with any redelivered message.
Original answer (before queue and stream queues were added):
There are redelivered
message property that set to true when message redelivered one or more time.
If you want to track redelivery count or left redelivers number (aka hop limit or ttl in IP stack) you have to store that value in message body or headers (literally - consume message, modify it and then publish it modified back to broker).
There are also similar question with answer which may help you: How do I set a number of retry attempts in RabbitMQ?
答案2
得分: 5
如果消息实际上被标记为死信,你可以检查x-death
消息头的内容。
例如,当你使用reject
/nack
并且队列有关联的死信交换时,就会出现这种情况。
在这种情况下,该头部的内容是一个数组。每个元素描述了一个失败的投递尝试,包含了尝试投递的时间、路由信息等。
这适用于RabbitMQ,我不知道它是否适用于AMQP的一般情况。
编辑
自从我最初写下这个答案以来,x-death
头部的结构已经改变。
头部改变格式通常是一件非常糟糕的事情,但在这种特殊情况下,原因是如果消息不断被标记为死信,消息的大小会无限增长。
因此,我已经删除了以前在这里获取消息死信次数的代码片段。
仍然可以从新的头部格式中获取死信次数。
英文:
In the case that the message was actually dead-lettered, you can check the contents of the x-death
message header.
This would for example be the case when you reject
/nack
with requeue = false
and the queue has an associated dead letter exchange.
In that case, the contents of this header is an array. Each element describes a failed delivery attempt, containing information such as the time it was attempted delivered, routing information, etc.
This works for RabbitMQ - I don't know if it is applicable to AMQP in general.
EDIT
Since I originally wrote this answer, the x-death
header structure has been changed.
It is generally a very bad thing that headers changes format, but
in this particular case the reason was that the message size would grow indefinitely if the message was continuously dead-lettered.
I have therefore removed the piece of code that used to be here to get the no of deaths for a message.
It is still possible to get the number of deaths from the new header format.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论