Search code examples
rabbitmqamqp

RabbitMQ: retrieve specific messages from the queue


I have trouble understanding the routing in RabbitMQ. Consider I have several producers (let call them clients) that produce messages to the queue. E.g., clients A, B, and C send messages to queue X1.

Let the consumer respond to all messages sending responses back to the queue. E.g., consumer gets message from queue X1, does something, and sends responses to the queue X1.

How can, client A determine where are in the queue X1 messages sent to it and where are messages sent to clients B or C?

I can't declare one queue per connection because of large number of connections expected (~10^6). So I'm in trouble here. Any suggestions? Thanks.


Solution

  • I think you need to look at the RPC tutorial. From your description it sounds like that is what you want to do. However that would probably require you to declare more queues than you want.

    Approaching this a different way. I cannot understand why you would send a reply back to the producer not only by the same exchange but the same queue that the consumers are consuming from.

    Would it not make sense to have producers P1,P2 and P3 send to exchange X1 with routing key "abc.aaa.xyz" / "abc.bbb.xyz" / "abc.ccc.xyz". Then have queues Q1, Q2 and Q3 bound to X1 with binding keys ".aaa." / ".bbb." / ".ccc." or just Q1 with binding key "abc.*.xyz" (I am unclear on exactly what you want so just making some suggestions). Which are consumed by Consumers C1, C2 and C3

    When the Consumer has finished processing the message then it will send a message to X2, with routing key that identifies itself. The producers will consume from queues bound to X2.

    The point I am trying to make is that you do not want more than one consumer reading from a queue. There is only one case in which you want that and that is a task queue. I am not clear on your use case so you may want a task queue. If you do then you should still not have your producers reading from the same task queue as your consumers. Aside from task queues you should have one consumer read from one queue. You may have many queues to one exchange and even many bindings from one queue to one exchange.

    I hope this helps