Skip to main content

RabbitMQ Java Client Example (Producer & Consumer)

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


Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

How to create a basic Spring 6 project using Maven

Below is a step-by-step guide to creating a basic Spring project using Maven. 1) Create a Maven Project Use the following Maven command to create a new Maven project. mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 2) Import in IntelliJ IDEA If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources..." . In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open." 3) Update pom.xml In total, the application requires the below-mentioned dependencies: 4) Create Spring Configuration Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration . 5) Create the Main Application C...