Reputation: 9466
I have a potentially slow operation and N workers that could perform this operation simultaneously. Using RabbitMQ, I'd like to publish a message which any of these N workers could handle, but which only 1 of the non-busy workers will actually undertake.
An analogy: There is a basket of apples and N hungry people. As soon as an apple is added to the box, only 1 person who isn't currently eating can take the apple. When all the people are eating, the apple will sit there until someone is ready to eat another. Once an apple is taken, no other person can have it, because the person who took it will not share. This is what I'd like to do with some type of RabbitMQ configuration.
Upvotes: 0
Views: 36
Reputation: 10182
You can use a direct exchange, set (consumer) prefetch count to 1 and akcnowledge the messages manually. This means that consumer get's one message at the time and when it's done processing it ACKs it and that's it.
so prefetch one - person takes one apple
rabbit mq dispatches messages in round-robbin manner - "next" person not eating will be handed next apple
manual ACK - person is finished eating and is waiting for next apple to be handed to her/him.
If the prefetch count was for example 2, this would mean that a person says I'll have two apples (so kinda reserve them) and while eating the first no one else is allowed to take the second one.
Similar use case is in the second tutorial, except that over there auto acknowledge is enabled.
Upvotes: 2