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

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...

How to become a Blockchain developer and write your first Smart Contract

Introduction This is an introductory article to help you understanding the tools and frameworks needed so that you can know from where and how to start creating your own Smart Contracts. In this post I will give you an overview of the tools, frameworks, libraries and languages used to create a Smart Contract in the Ethereum Blockchain . In the second part of this article, we are going to see how to create a Smart Contracts using Solidity and ee are also going to see how to run a Blockchain locally using Ganache , so that you can deploy, interact and test your Smart Contract in your local development environment. According to a definition from the Wikipedia website: A blockchain is a decentralized, distributed, and often public, digital ledger consisting of records called blocks that are used to record transactions across many computers so that any involved block cannot be altered retroactively, without the alteration of all subsequent blocks.. What do you need to know? T...

How to run OPA in Docker

From the introduction of the openpolicyagent.org site: OPA generates policy decisions by evaluating the query input against policies and data. In this post i am going to show you an easy and fast way to test your policies by running OPA in Docker. First, make sure you have already installed Docker and have it running: docker ps Inside your choosen directory, create two files. One called input.json file for your system representation and one file called example.rego for your rego policy rules. Add the following content to your json file: Add the following content for the example.rego: Each violation block represents the rule that you want to validate your system against. The first violation block checks if any of the system servers have the http protocol in it. If that is the case, the server id is added to the array. In the same way, the second violation block checks for the servers that have the telnet protocol in it and if it finds a match the server id is also...