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.
Thanks in advance
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.