问题描述
我实现了多个使用者,这些使用者从一个队列中获取消息,而我使用的方法类似于此示例,不同之处在于我正在执行basic.get进行无限循环轮询.
I implemented multiple consumers, who are fetching messages from a single queue, I am doing this using something similar to this example, except that I'm doing basic.get on an infinite loop for polling.
有人知道如何防止所有消费者之间的竞争,因为只有一个消费者会收到消息,而另一个将继续进行轮询,直到收到另一条消息为止?
我尝试实现一种逻辑,在该逻辑中,一旦我收到消息,便立即将其确认以删除该消息,但是似乎其他一些队列设法在第一个队列确认并删除之前获取了该消息.这样每个消费者都得到了消息.
Any idea how do I prevent racing between all consumers, in that only one consumer will get the message and the other will continue to do polling until another message comes?
I tried implementing a logic in which as soon as I get the message I ack it for the message to be removed, but it seems that some other queue managed to get the message before the first queue has ack and removed it.So each consumer got the message.
预先感谢
推荐答案
您无法以某种方式进行设置. RabbitMQ将循环轮询消息给使用者,但是只有一个使用者将从队列中接收消息.当您在一个队列中有多个使用者时,这是RabbitMQ中的设计.
you can't, the way you have things set up. RabbitMQ will round-robin the messages to the consumers, but only one consumer will receive the message from the queue. This is by design in RabbitMQ, when you have multiple consumers on a single queue.
如果需要所有使用者来接收所有消息,则需要更改配置,以便每个使用者都有自己的队列.然后,您需要通过交换发布消息,该交换会将消息传递给所有使用者的所有队列.
If you need all consumers to receive all messages, then you need to change your configuration so that each consumer has it's own queue. Then you need to publish your message through an exchange that will deliver the message to all of the queues for all of the consumers.
最简单的方法是使用Fanout交换类型.
The easiest way to do this is with a Fanout exchange type.
这篇关于Rabbitmq队列中有多个使用者-只有一个获得消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!