RabbitMQ with Java
There are two different communication ways which are sync and async communication. Today I’d like to discuss async communication and how it's related to RabbitMQ (for the rest of the post I’ll use Mq). Also, I’ll provide some pieces of code.
Mq is an open-source distributed message broker that works with Advanced Message Queuing Protocol (AMQP) protocol which is an open standard application layer protocol for message-oriented middleware. The defining features of AMQP are message orientation, queuing, routing (including point-to-point and publish-and-subscribe), reliability and security.
We are using it for different purposes in different services. Such as sending an email that is not necessary to be synced, and managing between some microservices processes. Some of our emails and notifications are not critical to send instantly. That’s why we use Mq here and queued our requests and when service is available then executes and sends them.
We prefer using Mq for communicating between services, for some specific reasons such as POST, UPDATE, and DELETE processes. Because we do not want to be affected by any other service’s problems in our service which we do process at that moment. Thus, we would manage each service's problems and responsibilities by its own process.
Using Mq provides us with some benefits such as;
- saves our time
- improves service performance
- reduces dependencies and their related problems
- separates responsibilities
Unfortunately, Mq is not a silver bullet and it has some drawbacks we faced;
- It is slow and takes time when proceeding with thousands of data.
- There is no way to move just a part of the messages in the queue to the destination queue. When you move messages, all of them are moved. I wish we could manage and send it partially.

Before jumping the code details I also want to add the model structure we used.
Model in brief;

There are two parts here — publisher and consumer. We separate our publisher and consumer and they are mostly located on different services. That’s why I separated the post into two-part here. But some settings are necessary for two parts which are dependency and application.yml configurations.
I used the 2.6.6 version of spring-boot-starter-amqp:
then added Mq configurations to my application.yml;
Publisher
We configure just exchange part in publisher and then send the message.
I configured Mq EXCHANGE as follows.
Note that I used TopicExchange here. TopicExchange definition which I quoted from RabbitMq address is;
“Topic exchanges 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. The topic exchange type is often used to implement various publish/subscribe pattern variations. Topic exchanges are commonly used for the multicast routing of messages.”
If you’d like to learn other exchange types, please check here.
After configuration now I am ready to publish my message to the exchange of that queue. For this, I used convertAndSend() method.
Consumer
Now I configured the queue configuration for the consumer part.
I configured Mq with QUEUE, EXCHANGE, and DEAD_LETTER_QUEUE definitions.
The code is almost ready. Now just one more step. I also need a listener method that I could consume the messages published.
@RabbitListener should be added to the top of the method with the required arguments.
Please be careful your publisher and consumer object that they should be the same.
The code is completely ready anymore.
Finally, you can find the test classes which I wrote for the classes above.
RabbitMqConfigurationTest
And here is the EnqueueService class test;
In the next post, I’m going to share another async communication example which is Kafka based on publish-subscribe streaming/messaging.
Hope it helps,
Thanks for reading…
References: