Skip to main content

Understanding RabbitMQ

Introduction

RabbitMQ is a centralized message broker based on the AMQP (Advanced Message Queuing Protocol) protocol, acting as a Middleware between Producers and Consumers of different systems.

In a message system, Publishers sends a message to a message broker where messages are consumed some time later by one or more Subscribers.

By introducing a message brokeer between systems we are decoupling the sender application from the receiver. In this case the service that is responsible for sending or publishing the message does not need to know about any other service. All it needs to care about is the message and its format.

With a message system you send a message to a message broker first and when the consumers or listeners of it become online they can start consuming from the message queue. This means you can keep sending messages without even care if the other application is online or if they had any failures.

RabbitMQ Architecture

Exchange, queue and bindings are the main components in the RabbitMQ architecture. In RabbitMQ, the message is first sent to an Exchange. In order for it to know which queue or queues the message should be route to we need to bind an Exchange to one or more Queues and provide a routing key for it.

In this way, Publishers just needs to know about the exchanges they are sending the messages to and the routing key that binds the exchange to a queue, while consumers just need to care about the queues itself.


Running RabbitMQ in Docker

If you want to be able to access the RabbitMQ Management Panel you need to run the following RabbitMQ docker image:
docker run -d --hostname rabbit --name rabbit -p 5672:5672 -p 5671:5671 -p 15672:15672 -p 15671:15671 rabbitmq:3-management

By exposing the port 15672 we can get access to the Management panel of RabbitMQ. The default username and password are both: guest.
To take advantage of Rabbit default configuration for Spring we are also exposing the port 5672. In this way we can use RabbitTemplate to send a message to our RabbitMQ message broker without providing any configuration so ever.

Creating the Exchange, Queue and Bindings

To create an exchange go to the Exchange top menu and click in the "Add a new exchange". Add a name for your new exchange and click in the Add exchange button to save your changes.
Click in the top Queue menu to create a new queue. Click in the Add a new queue and give a name for it. Click in the Add queue to save your changes.
To create the binding between the Exchange and the Queue, go back to the Exchange page and click in your Exchange name in the available table in the page.
Click in the Bindings section to enter a Queue to bind to this Exchange and enter a Routing key. Click in the Bind button to save your changes. We will need the routing key to send a message to the exchange from Spring using the RabbitTemplate.

Adding RabbitMQ to a Spring Boot project

To be able to use RabbitMQ and RabbitTemplate in Spring we need to add the following dependency:
implementation 'org.springframework.boot:spring-boot-starter-amqp'
testImplementation 'org.springframework.amqp:spring-rabbit-test'

Next we are going to create a Service class, called RabbitService. All you need to do in your new class is to inject the RabbitTemplate using Spring Dependency Injection by adding the @Autowired annotation in the property field as you can see in the following code.

The above convertAndSend method from the RabbitTemplate class will convert our Java Message object to an Amqp Message. As we said before, besides the message, we just need to pass the exchange name and the routing key to the method. That's all we need to do in order to publish a message to the RabbitMQ broker using Spring Boot.

If you start your Spring Boot Application you will note that Spring tries to connect to Rabbit at localhost:5672. That's why we exposed that port in our docker command in the beginning of this article.

Let's now add Swagger as an application dependency so that we can use it to send a message to our RabbitMQ.

Adding Swagger

To add Swagger, we just need to add the following dependency to our project:
implementation 'org.springdoc:springdoc-openapi-ui:1.6.4'

The Swagger UI shows us all the resources available from our API, as we can see in the image below:
Besides providing all the API documentation for other developers that need to interact with the API, we can also make use of the Swagger UI to test our endpoints by accessing the following url:
http://localhost:8080/swagger-ui/index.html

From the image we can see that we have a message resource that we can interact with by sending a POST request to it. If you open that resource in the Swagger UI and click in the Try it out button you will see that Swagger provides the json object required for the payload. We just need to fill with the desired values to test our application API from the browser using the Swagger UI editor.
After executing the POST request using the Swagger UI we can see that the server responded with an HttpStatus code 201, indicating that our request completed successfully, since that is the status code that we are returning from the controller.

Getting the message from the Rabbit Management panel

To see that our message indeed was successfully sent to our message queue we can go back to the RabbitMQ Management panel and click in the Queues menu once again. The first thing you will notice is that the Queue graph now shows that we have one message ready to be consumed.
We can now go to the Get messages section and enter the number 1 in the Messages input field in the form and click in the Get Message(s) button to view it.

Conclusion

In the next article we are going to talk more about other configurations options. This time is important to interact with the Rabbit Management panel so that we can understand later what the configuration is doing for us. This understanding and manual creation of Exchanges, Queues and Bindings will be of great knowledge whenever you need to check why is a message not being sent to a certain queue.

In the next article, we will also cover the consumer part of RabbitMQ with Spring Boot.

Github project

GitHub Repo

References

AMQP Concepts
SWAGGER UI
SPRING GUIDE TO RABBITMQ
SPRING INTRO TO RABBITMQ
RABBITTEMPLATE DOC

Comments

Popular posts from this blog

Log Aggregation with ELK stack and Spring Boot

Introduction In order to be able to search our logs based on a key/value pattern, we need to prepare our application to log and send information in a structured way to our log aggregation tool. In this article I am going to show you how to send structured log to ElasticSearch using Logstash as a data pipeline tool and how to visualize and filter log information using Kibana. According to a definition from the Wikipedia website: Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents. According to Elasticsearch platform website , Elasticsearch is the heart of the Elastic stack, which centrally stores your data for lightning fast search. The use of Elasticsearch, Kibana, Beats and Logstash as a search platform is commonly known as the ELK stack. Next we are going to start up Elasticsearch, Kibana and Logstash using docker so we can better underst

Selection Sort Explained

Introduction If you are trying to get a remote job in a top IT consulting company, you will definitely fall into a live code exercise where your algorithms, logical thinking and problem solving skills will be tested and you will have to demonstrate a solid knowledge of these concepts. Today I decided to write about a type of sorting algorithm that I found several times in interviews and decided, after studying the approach used, to create an initial solution in the simplest possible way. Understanding the logic As we know, the sort algorithm basically uses three basic principles to sort the items in a list. A comparator, a swap function, and recursion. For this selection sort algorithm I will focus in the first two. Given that we have the following list of numbers: 64, 25, 12, 22, 11, how would we use selection sort to swap and sort the list in an ascending order? The following code from the init function uses two for loops to create a temporary list (line 2) with the r

How to use Splunk SPL commands to write better queries - Part I

Introduction As a software engineer, we are quite used to deal with logs in our daily lives, but in addition to ensuring that the necessary logs are being sent by the application itself or through a service mesh, we often have to go a little further and interact with some log tool to extract more meaningful data. This post is inspired by a problem I had to solve for a client who uses Splunk as their main data analysis tool and this is the first in a series of articles where we will delve deeper and learn how to use different Splunk commands. Running Splunk with Docker To run Splunk with docker, just run the following command: docker run -d —rm -p 8000:8000 -e SPLUNK_START_ARGS=--accept-license -e SPLUNK_PASSWORD=SOME_PASSWORD --name splunk splunk/splunk:latest Sample Data We are going to use the sample data provided by Splunk. You can find more information and download the zip file from their web site . How does it work? In order to be able to interact with Splunk t