Spring Boot Messaging with RabbitMQ

Hasan Kadir Demircan
4 min readApr 1, 2023

What is the RabbitMQ?

Messaging enables software applications to connect and scale. Applications can connect to each other, as components of a larger application, or to user devices and data. Messaging is asynchronous, decoupling applications by separating sending and receiving data.

You may be thinking of data delivery, non-blocking operations or push notifications. Or you want to use publish / subscribe, asynchronous processing, or work queues. All these are patterns, and they form part of messaging.

RabbitMQ is a messaging broker — an intermediary for messaging. It gives your applications a common platform to send and receive messages, and your messages a safe place to live until received.

Let’s look at the keys of the RabbitMQ
Producer: An application sends and generates messages to the queue. It could be a third party or your microservice while sending a message to another microservice.
Consumer: An application to consume/process the message in the queue it is listening to. ( the message comes from Producer.)
Queue: It is the list where messages are added, consumed, and kept.
Exchange: The producer does not send the message it produces directly to the receiver or queue, there is a message router in between, this is the structure that performs the routing process. The producer transmits the message it produces to the exchange, the exchange adds the incoming message to the queue with the relevant information, and if there is a consumer listening, it receives the next message from the queue for processing. In short, its task is to transmit the message it receives according to the specified Routing Key to the relevant Queue.
Binding: It is the routing rule of the connection to be established between Exchange and Queue. Exchange distributes the messages it receives to the corresponding queues according to this rule.
Routing Key: It is the notification of the message produced by the producer and forwarded to Exchange by marking which queue it will be forwarded to.
Exchange Type: It is the type that specifies the method according to which the message will be transmitted to the queue.

Exchange Types

  • Direct Exchange
  • Topic Exchange
  • Fanout Exchange

Direct Exchange
When sending the message, it is transferred to the corresponding Queue with the Binding (key specified when assigning the Queue to an Exchange) that matches the routing key specified. It is also the exchange type used by default, each Queue is automatically assigned to this exchange type with a routing key.
It uses this exchange type if you want to send a message to only one(1) customer.

Topic Exchange
Topic exchange, unlike direct exchange, can forward messages to more than one Queue. Likewise, the routing key in the sent message is transferred to the queue with the matching Binding key. However, the binding key and the routing key must consist of words separated by dots. (There is no such requirement in Direct Exchange.) The word can be any text up to 255 bytes. There are two special cases that we are familiar with from regex when defining the binding key.
* (asterisk): Can replace a word where used.
# (hash): Where used, it can replace zero or more words.

Fanout Exchange
Messages sent in Fanout Exchange are copied and sent to all connected queues regardless of the routing key. In this type of exchange, the routing key does not matter.

Let’s check the example code;
application.properties;

Only added some basic queue names.

RabbitMqConfig;

QueueConfig;

in this config file, we bind the queues such as direct, topic, and fanout.

GenericPushlisher.java

  • We will call GenericPublisher from our service to publish the messages to RabbitMQ

Controller;

  • in this case, we imagine, when we get a user login or register request, we will send this information to RabbitMQ.
    So why? Because let’s imagine when we save user data into our DB. Then we would like to send a welcoming email to the user or store the user's steps on 3rd party application.

UserService;

  • When we call the service from the controller, the service publishes our message to the queue in order to exchange-type.

Consumer;

  • in this class, we will get the messages that we send from the publisher :)

In this project, we implement publisher and consumer in one project.
If you want to implement different microservices/projects.

You can check my GitHub, I also added a different project to consume the messages.

And if you want to run the project, you should have a rabbitMQ.
For it, go to my GitHub and you will find docker-compose.yml file.
To run the file, follow the script below.

$ docker-compose -f docker-compose.yml up -d

Thank you for reading here :)

--

--