{getToc} $title={Table of Contents}
{getCard} $type={post}
Reactor RabbitMQ in your Reactive Spring Boot Application — Part 4 Final |
Introduction
This the final part of this tutorial: I'm working on receiver service module
which is the last part of this tutorial. The project structure is very similar
to sender module.
Microservice Architecture with RabbitMQ
Microservice Architecture with RabbitMQ |
I have implemented the receiver rabbitmq configuration named as
RabbitMQReceiverConfiguration.
I have created two model classes product & order product same as in
Sender Service.
We don’t need to create router and handler functions but eventually, for the
application, you would need to expose other endpoints. What we need is to
set up a service that will be consuming and listening to the RabbitMQ. So I
have implemented the service named as ProderOrderService.
There are a few things to do in Service class.
* Listen to the queue
* Deserialize byte to JSON
* Deserialize JSON to the Order object
* Do your business logic, you can save the object in your database for
example.
* Close connection
// Make sure to close the connection before the program is finished
@PreDestroy
public void close() throws IOException {
connMono.block().close();
}
In this service class, we use @PostConstruct, so it will listen to
RabbitMQ as soon as the service is up. Using the @PostConstruct we
can call our consume method after the dependency injection is done.
// Listen to RabbitMQ as soon as this service is up
@PostConstruct
private void init() {
consume();
}
And we close the connection before the program is finished by using
@PreDestroy to close connection.
In Part 1:
I have built spring boot project from scratch. And I will categorize with 3
module: First module is parent pom module as wrapper project,
second is the sender module for sender service to send message to rabbitmq
broker and the last one is the receiver module for receiver queue
message
from rabbitmq broker.
In Part 2:
I have implemented RabbitMQ configuration for the sender. And I have create
the connection, senderOptions, and sender beans.
I have used Router configuration to use Spring Webflux to its full potential
I believe it is better to move away from the controller
annotation-based and move towards using functions to route and handle
requests.
And I have talked about what is Spring WebFlux?
I have created model and service for sending message queue to rabbitMQ.
The product order model, is made of a collection of products and also has a
total cost associated with it. Because this total cost is calculated when
the object is created it makes sense to create a Product DTO with a flat
structure that will be used to transport the data from the client to the
server and will only calculate the total value after the object has been
validated.
I have created product service in order to create sender method to send
message queue.
The queue message in RabbitMQ Broker named as "spring-reactive-queue"
I have created product handler service to receive POST request from the
router.
The purpose of this handler is to take the server request coming from the
router functions so that we can extract any information that the request may
have, call our service for the business logic and eventually return a server
response for the client.
I have created senderOrder method is to extract the JSON object from the
server request. With the DTO extracted we can send it to the service and
then finally return the server response with a 201 status and the mono order
inside the body.
In Part 3:
I have worked on setting sender properties port and implemented close
connection in main application.
I have run project demo to send message queue with application on the sender
service that I have already implemented from part 1 & 2.
I have demonstrated about how to build rabbitmq image on docker service and
run service on localhost. I have request sender service to send message
queue and store queue on rabbitmq message queue.
RabbitMQ
is a message broker: it accepts and forwards messages. You can think about
it as a post office: when you put the mail that you want posting in a post
box, you can be sure that the letter carrier will eventually deliver the
mail to your recipient. In this analogy, RabbitMQ is a post box, a post
office, and a letter carrier.
Message Broker
It is an intermediary computer program that applications and services use to
exchange information with each other. It can be used to store, deliver and
route messages. It is important to understand that it is an intermediary
computer program. This means that it doesn’t know if the services are online
or offline and doesn’t know how many recipients there are, but it still
ensures that the recipients receive the messages.
RabbitMQ Message Broker Diagram |
Queue
The messages received from the producers will be kept in a message queue,
and as soon as the consumers are ready to receive them, they will consume
the messages. That is how the message broker ensures that the recipient
receives a given message.
To Run Application:
- Firstly, you need start up RabbitMQ service by running and build image on
docker service.
- Secondly, run our sender service application as we have done in part 3,
- Lastly, run our receiver service application and get ready to consume the
message that we stored before.
RabbitMQ image running on docker on localhost |
Request Sender Message via Postman
RabbitMQ running on localhost
One message queue is storing in rabbitmq queue.
Hope this video can bring you with new idea and happy day...
Help to subscribe, like and command to support my work.
Conclusion:
This tutorial has been created to help you understand RabbitMQ, Spring
WebFlux, and finally Reactive RabbitMQ.
If you ever have to implement the Reactive RabbitMQ but you can’t remember
exactly how to do it, just remember to check this tutorial from the
beginning.