本文介绍了RabbitMQ - 如何处理过期队列中的死信/处理消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设置了 x-expires 的队列.我遇到的问题是我需要对队列中的消息进行进一步处理IF 队列过期.我最初的想法是在队列上设置 x-dead-letter-exchange.但是,当队列到期时,消息就会消失,而不会进入死信交换.

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?

推荐答案

正如评论中所建议的,您不能仅依靠 x-expire 功能来做到这一点.但是在我遇到的类似情况下有效的解决方案是:

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. 使用 x-message-ttl 确保消息在不及时消费的情况下死亡,
  2. 为所有这些消息将被路由的队列分配一个死信交换,
  3. 使用 x-expires 将队列过期时间设置为高于消息 TTL 的值,
  4. (这是棘手的部分)假设您可以控制您的消费者,在最后一个消费者离线之前,删除与您的垂死"队列的绑定,可能是通过 REST API 调用 - 这将阻止新消息被路由到队列.
  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.

这篇关于RabbitMQ - 如何处理过期队列中的死信/处理消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 19:36