0
votes

I'm a bit confused about RabbitMQ best practices regarding the use of Queues and Exchanges. Let's say I would like to deliver a GenerateInvoice message with some data for an invoice, and have multiple consumers processing the invoice data and generate a PDF. Each GenerateInvoice should be processed only by one consumer.

One approach is to declare an queue and publish the GenerateInvoice messages to this queue and let all consumers consume from this queue. That would distribute the message across the different consumers.

It's unclear to me, if the above is okay or best practice is delivering the messages to a Exchange instead of publishing them directly to a queue. Using an Exchange I have to ensure that an queue is declared after the producer has created the Exchange before it starts to publish messages. Otherwise no queue would receive the messages and the message would be lost.

1

1 Answers

1
votes

Declaring a queue, publishing GenerateInvoice messages to the queue and having multiple consumers for the queue would work in this scenario.

The messages published to the queue will not be lost and they stay on RMQ if there are no consumers. Only thing is to make sure queue is declared before messages are published.

Java Example:

channel.queueDeclare(QUEUE_NAME, false, false, false, null);

Then, Publish can be done as:

channel.basicPublish("", QUEUE_NAME, null, message.getBytes());

and consume can be done as:

channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });