为什么我在重新排队时从 RabbitMQ 收到相同的消息?

Why do I get the same message from RabbitMQ when requeueing?

背景

我的队列中有两条消息:

我有一个通道,一个 Qos 处于活动状态且预取计数为 1 的消费者。

场景

一条消息投递foo进来了,我用requeue=true拒绝了消息。

预计: 下一条传送的消息是 bar

实际: 下一条传送的消息是foo

问题:

为什么我的消费者收到的是它刚刚重新排队的消息,而不是下一条?我怎样才能得到上述结果?

我希望重新排队的消息位于队列的尾部。

我考虑过确认并重新发布该消息,但这会带来丢失消息的风险。

来自https://www.rabbitmq.com/semantics.html

Messages can be returned to the queue using AMQP methods that feature a requeue parameter (basic.recover, basic.reject and basic.nack), or due to > a channel closing while holding unacknowledged messages. Any of these scenarios caused messages to be requeued at the back of the queue for RabbitMQ releases earlier than 2.7.0. From RabbitMQ release 2.7.0, messages are always held in the queue in publication order, even in the presence of requeueing or channel closure.

强调我的

因为你只有一个消费者,所以这种行为是意料之中的

您可以将消息重新发布到同一队列, 这将确保问题消息结束。

您的另一个选择是为失败的消息创建一个新队列,这样您就可以 分别处理。