Integrate the message broker RabbitMQ with spring boot
1. What is RabbitMQ?
RabbitMQ is a message broker or queue manager that implements the AMQP protocol.
This means that he takes messages in a queue and sends them to consumers.
A message can include any kind of information.
Producer: publishes a message to an exchange.
message: any kind of information or data.
Queue: hold many messages, queue (first in, first out).
Broker: in our case the RabbitMQ, a broker can have many queues.
Consumer: is the system that receives the message from the broker.
With a message broker, we can decouple our application easily.
This means we use a microservice architecture.
And let’s imagine that the Email Service is unavailable
that will not affect the other services, and the application still works.
2. What is an exchange:
RabbitMQ does not publish directly the messages to a queue but via an exchange.
Exchange: assure that the message is published to the right queue.
Each exchange is linked to a queue via binding.
3. Types of exchange:
There are 4 types of exchanges:
Direct exchange: direct the message to a specific queue by locking to the routing_key.
Fanout Exchange: a fanout exchange routes messages to all queues that are bound to it, and the routing key is ignored.
Headers Exchange: is designed for routing on multiple attributes that are more easily expressed as message headers than a routing key. Headers exchanges ignore the routing key attribute. Instead, the attributes used for routing are taken from the headers attribute.
Topic Exchange: route messages to one or many queues based on matching between a message routing key and the pattern that was used to bind a queue to an exchange.
3. Install RabbitMQ:
In this post I will use the docker image:
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
Now navigate to:
By default, RabbitMQ creates a guest user.
4. Integrate RabbitMq with Spring boot:
Create Spring boot application:
Dependencies: Spring Web, Spring Boot DevTools, and Spring for RabbitMQ.
Now let’s create a config package and inside it create RabbitMqConfig class
and define some beans:
Next, let's create RabbitMqService:
In this service, we have 4 methods: each one represents an exchange type.
Next, let’s create a RestController:
To receive messages from RabbitMQ, we can use the RabbitListener and pass the queue name.
Inside the MqController :
Note: after adding the recivedMessage() method, you will see the result in your console and not on the RabbitMQ dashboard because RabbitMQ saves the messages until it is received by the consumer.
Add the application.properties :
Now we will test our APIs
Open Postman with HTTP method GET:
And now navigate to http://localhost:15672/#/queues
You see that we have a queue demo-rabbitmq.
Click on demo-rabbitmq and then Get messages:
Now let’s test the Header Exchange :
open Postman with HTTP method GET:
Now click on Get messages again you will find 2 messages:
For the other 2 exchange on postman :
and repeat the same steps as before.
6. Last word:
You can find the code from GitHub :
This is a RabbitMQ integration with Spring Boot. Contribute to HamdiBouallegue/rabbit-mq development by creating an…
AMQP 0-9-1 Model Explained
This guide provides an overview of the AMQP 0-9-1 protocol, one of the protocols supported by RabbitMQ. AMQP 0-9-1…
If you enjoyed this article, please clap it up 👏 and share it so that others can find it! Follow me to get more of me 😄.