Distributing messages to many consumers

In this example we are showing how to create a dynamic load balancer, and how to distribute messages to many consumers. We are going to create a file downloader.

You can find the source at Chapter01/Recipe10/Java_10/.

Getting ready

To use this recipe we need to set up the Java development environment as indicated in the Introduction section.

How to do it…

In order to let two or more RabbitMQ clients properly balance consuming messages, you need to follow the given steps:

  1. Declare a named queue, and specify the basicQos as follows:
    channel.queueDeclare(myQueue, false, false, false,null);
    channel.basicQos(1);
  2. Bind a consumer with explicit ack:
    channel.basicConsume(myQueue, false, consumer); 
  3. Send one or more messages ...

Get RabbitMQ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.