简体   繁体   中英

How to publish to multiple queues with work queue behavior?

Using RabbitMQ, I have two types of consumers: FileConsumer writes messages to file and MailConsumer mails messages. There may be multiple consumers of each type, say three running MailConsumers and one FileConsumer instance.

How can I do this:

  • Each published message should be handled by exactly one FileConsumer instance and one MailConsumer instance
  • Publishing a message should be done once, not one time for each queue (if possible)
  • If there are no consumers connected, messages should be queued until consumed, not dropped

What type of exchange etc should I use to get this behavior? I'd really like to see some example/pseudo-code to make this clear.

s列

This should be easy to do, but I couldn't figure it out from the docs. It seems the fanout example should work, but I'm confused with these "anonymous queues" which seems like it will lead to sending same message into each consumer.

If you create queue without auto-delete flag, then queues will stay alive even after consumers disconnection.

Note, that if you declare queue as persistent, it will be present even after broker restart.

If you will publish then messages with delivery-mode=2 property set (that mean that message will be persistent), such messages will stay in persistent (this is important to make queue persistent) queues even after broker restart.

Using fanout exchange type is not mandatory. You can also use topic for better message routing handling if you need that.

UPD: step-by-step way to get what you show with schema.

  1. Declare persistent exchange, say main , as exchange.declare (exchange-name=main, type=fanout, durable=true) .
  2. Delcare two queues, say, files and mails as queue.declare (queue-name=files, durable=true) and queue.declare (queue-name=mails, durable=true)
  3. Bind both queues to exchange as queue.bind (queue-name=files, exchange-name=main) and queue.bind (queue-name=mails, exchange-name=main) .

At this point you can publish messages to main exchange (see note about delivery-mode above) and consume with any consumer number from queues, from files with FileConsumer and from mails with MailConsumer . Without any consumers on queues messages will be queued and stay in queue until they consumed (or broker restart is they are not persistent).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM