问题
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 trying to follow a logic in which as soon as I get the message I ack it for the message to be removed, but its seems that some other queues, manage to get the message also before the first has ack and removed the message.
So everyone got this message.
Thanks in advance
回答1:
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?
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.
The easiest way to do this is with a Fanout exchange type.
来源:https://stackoverflow.com/questions/32864644/rabbitmq-multiple-consumers-on-a-queue-only-one-get-the-message