如何重新传递 RabbitMQ 已经发送给消费者的消息

How to redelivery messages that RabbitMQ already sent to consummers

我在 Rabbitmq 中创建了一些连接到队列的消费者,解开 X 条消息,例如每次 10,50,100 条,以避免不必要的连接一条接一条。 有时我们会遇到队列几乎为空并且只有一个消费者收到所有消息的情况。不幸的是,其中一条消息的处理速度可能很慢(例如,第三方 Web 服务超时),而所有其他消息必须排队等待这条消息完成,即使它们更快。 虽然这样,其他消费者是空的,无事可做,但他们不能接受第一个还没有处理的消息。

如果我可以告诉 Rabbitmq 向消费者发送某种消息,如果一段时间后它没有确认,则必须将消息发送到队列并由另一个消费者接收。 有人知道是否有解决方法吗?

看看我的

Consider the following scenario:

  • A queue has thousands of messages sitting in it
  • A single consumer subscribes to the queue with AutoAck=true and no pre-fetch count set

What is going to happen?

RabbitMQ's implementation is to deliver an arbitrary number of messages to a client who has not pre-fetch count. Further, with Auto-Ack, prefetch count is irrelevant, because messages are acknowledged upon delivery to the consumer.

Thus, every message in the queue at that point will be delivered to the consumer immediately and the consumer will be inundated with messages. Assuming each message is small, but takes 5 minutes to process, it is entirely possible that this one consumer will be able to drain the entire queue before any other consumers can attach to it. And since AutoAck is turned on, the broker will forget about these messages immediately after delivery.

Obviously this is not a good scenario if you'd like to get those messages processed, because they've left the relative safety of the broker and are now sitting in RAM at the consuming endpoint. Let's say an exception is encountered that crashes the consuming endpoint - poof, all the messages are gone.

我相信您看到的是 AutoAck 设置为 true 的情况。发生这种情况时,如果在它有机会连接之前没有其他消费者连接,则第一个连接的消费者将耗尽整个队列。尝试将 AutoAck 设置为 false,然后 select 一个合理的 pre-fetch 计数(可能是 0-1?),您将不会看到这种行为继续存在。

Autoack 是错误的,但我收到了很高的 pre-fetch,比如 50 条消息,因为我想减少对 Rabbit 的调用。 目前我将预取更改为 5,因此当一条消息冻结时,消费者将只保留 5 条消息。我正在监视 Rabbit 服务器的性能,但我担心高峰时刻的消耗。 感谢 theMayer 的帮助。