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:

http://localhost:15672/#/

By default, RabbitMQ creates a guest user.

Login: guest

Password: guest

4. Integrate RabbitMq with Spring boot:

Create Spring boot application:

https://start.spring.io/

Dependencies: Spring Web, Spring Boot DevTools, and Spring for RabbitMQ.

pom.xml:

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:

http://localhost:8888/api/send

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:

http://localhost:8080/api/send-headers

Now click on Get messages again you will find 2 messages:

For the other 2 exchange on postman :

GET: http://localhost:8080/api/send-direct

GET: http://localhost:8080/api/send-fanout

and repeat the same steps as before.

6. Last word:

You can find the code from GitHub :

Useful links:

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

I'm Hamdi, a software engineer 👨‍💻, and writer 📝