Reactor RabbitMQ in your Reactive Spring Boot Application — Part 2

{getToc} $title={Table of Contents}


Part 2




{getCard} $type={post}

In part 1, I have demonstrated about RabbitMQ Message Broker Process and the microservice architecture with RabbitMQ Message Broker Diagram and do the final project demo.


Implement Sender Service.

I have initialized spring boot project from scratch. And I categorized 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. 


Project Structure


In RabbitMQ Configuration

I have implemented RabbitMQ configuration for the sender. I have create the connection, senderOptions, and sender beans.


RabbitMQ Configuration



In Router Configuration

I have used Router function 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.

Router Configuration


I have defined the end-point of the Sender Service.
This class has to be annotated with @Configuration. Inside this class, we will create a bean called routerFunction that will route the incoming requests to the handler. It works just like the controller that you may be used to.


What is Spring Webflux ?

Spring WebFlux is a new functional web framework built using reactive principles.
The original web framework included in the Spring Framework, Spring Web MVC, was purpose-built for the Servlet API and Servlet containers. The reactive-stack web framework, Spring WebFlux, was added later in version 5.0. It is fully non-blocking, supports Reactive Streams back pressure, and runs on such servers as Netty, Undertow, and Servlet 3.1+ containers.


Models and DTO

I have created class for Product & Product order model, Product DTO class.

Product DTO



Product DTO Class

Product Model


Product Model

Product Order Model

Product Order MOdel


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.



Product Sender Service

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"


Product Service (1)


Use an ObjectMapper to map the order instance as a JSON string and then serialize the JSON string to bytes.

With the order serialized we can create the outbound message, and then finally push the message to the queue.


Product Service (2)


I have created a private mapperProductDTOtoEntity in this product service in order to use an ObjectMapper to map the product order instance as a JSON string.


Mapper Function



Product Handler

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.


Product Handler



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.


Watch Full Video of Part 2




Previous Post Next Post