Simple Pub-Sub implementation with Spring Boot, Docker, and RabbitMQ

How to set up a simple pub-sub with manual acknowledgment

Photo by Joel Holland on Unsplash

Publish-Subscribe-Pattern

First, let’s start with some quick fundamentals about the Publish-Subscribe-Pattern. The pattern is very simple but powerful. The following image shows the main concept:

Publish-Subscribe-Pattern
  • Subscriber: Subscribes to specific topics or a specific queue and consumes incoming messages.

Acknowledgment

We don’t care who exactly processes the message but that somebody does it. Therefore we make use of acknowledgment. If you have a use-case where you don’t need every message to be processed, you can skip this and go for a fire-and-forget approach.

RabbitMQ setup

Make sure you have Docker installed. If that is the case just open your terminal and type in the following command:

Publisher Spring Boot Application

To set up our project we will be using spring initializr. Just go for the same setting as I did (see the screenshot below) or use your custom own to fulfill your personal requirements. For this tutorial, we will only need the Spring for RabbitMQ and Spring Web dependency.

Spring initializr screenshot

Manually adding the dependencies

If you want to use RabbitMQ in an already existing Spring Boot application, for Gradle open up your build.gradle and add the following to your dependencies:

implementation 'org.springframework.boot:spring-boot-starter-amqp:2.4.2'
implementation 'org.springframework.boot:spring-boot-starter-web:2.4.2'
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.2</version>
</dependency>

Adding the configuration

As a real first step for the project, we want to add a decent configuration. Therefore just create a new folder and call it config. Inside of this folder create a new Java file and call it RabbitMQConfig:

Example Controller

Add a new folder and call it event. Next, add a new Java class and call it DemoEventController. Add the following code:

Run the application

Execute the bootRun task. Your application should startup. Assuming you are running the Spring Boot application on port 8080, post the following command into your terminal:

Screenshot of the RabbitMQ manager after publishing the example message

Manual acknowledgment

To make sure that a message gets processed by a consumer we want to set the queue to manual acknowledgment mode.

Subscriber Spring Boot Application

Just clone the Publisher project we just set up. Delete the DemoEventController and add a new class DemoEventConsumer:

Acknowledgment

Both parameters in combination can be used to manually acknowledge the message. As you can see we first print out the received message. Next, we try to acknowledge the message by using the delivery tag. If the basicAckcall succeeds the message is set to acknowledged in the RabbitMQ management console. If the method fails and throws an exception the basicNack method gets called which rejects the message. Nack is the abbreviation for negative acknowledgment.

RabbitMQ management console Unacknowledged message screenshot

Result

If everything works as expected, after calling our endpoint from the Publisher endpoint, you now should see an output in the following format in your Consumer application log:

Received message in the consumer application

Conclusion

In this tutorial, I showed you how to set up a simple publisher-subscriber pattern with RabbitMQ, Spring Boot, and Docker.

Passionated Mobile & Backend Developer, besides tech I write about fitness, health, and self-development

Get the Medium app