RabbitMQ Java Client Example (Producer & Consumer)

  • 4.5/5
  • 4595
  • Jul 20, 2024

RabbitMQ is a message broker: it accepts and forwards messages.

It consists of a large message buffer (queue), a queue is only bound by the host's memory & disk limits.

Many producers (program that sends messages) can send messages that go to one queue, and many consumers (a program that waits to receive messages) can try to receive data from one queue.

Installing RabbitMQ

In order to complete this article, we need a running instance of "RabbitMQ".

There are many ways of downloading and installing RabbitMQ.

For the sake of simplicity, let's start a RabbitMQ instance with the help of a Docker image.

If you have Docker installed on your machine, you can run a RabbitMQ instance with the following command:

% docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.11.9

There are a number of clients for RabbitMQ in many different languages.

In this article, we'll use the Java client provided by RabbitMQ.

RabbitMQ with Java

There are two kinds of applications interacting with a messaging system: producers and consumers.

In this article, we will discuss a simple example with two services (producers and consumers) which will communicate using RabbitMQ.

One of the service (producer) will publish messages to RabbitMQ and the other one (consumer) will consume.

1) Producer

1.1) Dependencies

The final "pom.xml" should look something like this:

1.2) Implementation

In the code below, first we establish a connection to RabbitMQ using "ConnectionFactory".

Secondly, we declare a queue named "order_queue".

Next, we produce 10 random messages and publish them to the queue.

Finally, we terminate the connection to RabbitMQ.

1.3) Testing

Now that we've completed our Producer configuration, run the application and you should see the following output in the console:

Produced: {"oderId":1744, "items":[{"id":1730},{"id":6}]}
Produced: {"oderId":1217, "items":[{"id":1931},{"id":8}]}
Produced: {"oderId":1044, "items":[{"id":1807},{"id":6}]}
Produced: {"oderId":1183, "items":[{"id":1958},{"id":1}]}
Produced: {"oderId":1442, "items":[{"id":1254},{"id":7}]}
Produced: {"oderId":1431, "items":[{"id":1963},{"id":3}]}
Produced: {"oderId":1931, "items":[{"id":1334},{"id":5}]}
Produced: {"oderId":1095, "items":[{"id":1191},{"id":9}]}
Produced: {"oderId":1464, "items":[{"id":1537},{"id":5}]}
Produced: {"oderId":1792, "items":[{"id":1324},{"id":9}]}

2) Consumer

2.1) Dependencies

The final "pom.xml" should look something like this:

2.2) Implementation

In the code below, first we establish a connection to RabbitMQ using "ConnectionFactory".

Secondly, we create a "DefaultConsumer" with an anonymous override implementation of "handleDelivery()".

Finally, we tell this consumer to listen to "order_queue" and consume from it.

2.3) Testing

Now that we've completed our Consumer configuration, run the application and you should see the following output in the console:

Consumed: {"oderId":1744, "items":[{"id":1730},{"id":6}]}
Consumed: {"oderId":1217, "items":[{"id":1931},{"id":8}]}
Consumed: {"oderId":1044, "items":[{"id":1807},{"id":6}]}
Consumed: {"oderId":1183, "items":[{"id":1958},{"id":1}]}
Consumed: {"oderId":1442, "items":[{"id":1254},{"id":7}]}
Consumed: {"oderId":1431, "items":[{"id":1963},{"id":3}]}
Consumed: {"oderId":1931, "items":[{"id":1334},{"id":5}]}
Consumed: {"oderId":1095, "items":[{"id":1191},{"id":9}]}
Consumed: {"oderId":1464, "items":[{"id":1537},{"id":5}]}
Consumed: {"oderId":1792, "items":[{"id":1324},{"id":9}]}

Source Code

Index
Apache Kafka : Install Zookeeper ensemble on AWS EC2

3 min

Apache Kafka : Developing Producer & Consumer in Java

10 min

Apache Kafka : Best Practices-Topic, Partitions, Consumers and Producers

2 min

Apache Kafka : Setup Multi Broker Kafka Cluster on Amazon EC2

3 min

RabbitMQ Java Client Example (Producer & Consumer)

5 min

Elasticsearch : Introduction to Spring Data Elasticsearch

7 min

Elasticsearch : Getting started with Elasticsearch & Kibana

3 min

Getting started with Spring Data MongoDB (Spring Boot + MongoDB)

11 min

Spring Data Redis with RedisTemplate and CrudRepository

14 min