How to create a Simple RabbitMQ listener in Spring boot ?

How to create a Simple RabbitMQ listener in Spring boot ?

How to create a Simple RabbitMQ listener in Spring boot ?

 

We are going to create a simple rabbitmq listener in the same spring boot project, but general usecase would be to use rabbitmq is between different microservices / different spring boot projects.

This is to make a understanding of how to use rabbitmq and what are all things we need to do for a rabbitmq connectivity.

 

Prerequisite:


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

Step 1: Declare a queue name in application.properties file
Create (or) automatically enable spring boot project to create the queue when not exist in rabbitmq.

Add the below entries in your application.properties file.

[plain]
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=USERID // web or guest default
spring.rabbitmq.password=PASSWORD // web or guest default
spring.rabbitmq.listener.simple.acknowledge-mode=auto
yourqueue.simple=my_simple_queue_name
[/plain]

If your rabbitmq is going to provide the communication between different spring boot projects/microservices then add the below entries to the other side/listener side as well.

[plain]
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=USERID // web or guest default
spring.rabbitmq.password=PASSWORD // web or guest default
spring.rabbitmq.listener.simple.acknowledge-mode=auto
spring.rabbitmq.listener.simple.concurrency=15
spring.rabbitmq.listener.simple.max-concurrency=15
spring.rabbitmq.listener.direct.default-requeue-rejected=false
spring.rabbitmq.listener.simple.default-requeue-rejected=false
yourqueue.simple=my_simple_queue_name
[/plain]

 

 

Step 2: Write a service class to send / send and receive a request

Here request argument is my input to be passed out to my receiver microservice through rabbitmq. If you a object then convert that to JSON string and pass here.

Same way it can be converted back to object in receiver microservices.

<code>@Service (or) @Component</code>
<code>public class Sender {</code>
<code>// yourqueue.simple is referred from application.properties</code>
<code>@Value("${yourqueue.simple}")</code>
<code>String myQueuName;</code>

<code>@Autowired</code>
<code>private RabbitTemplate rabbitTemplate;</code>

<code>public String sendMessage(String request){</code>
<code>rabbitTemplate.convertAndSend(queue, request);</code>
<code>}</code>
<code>}</code>

 

 

Step 3: Write a consumer or listener to receive the request from rabbitmq

Here @RabbitListener is written for this particular queue using @Queue annotation, so whenever message is posted/sent to the queue this listener will get triggered.

Json can be converted to real object type and passed to service class for further business.

@Service
public class Receiver {

@RabbitListener(queuesToDeclare = { @Queue("${yourqueue.simple}") })
public void getMessageFromSender(Message<String> responseBody) {
try {
MyCustomBean myCustomBeanRequest = (MyCustomBean) Utils
.convertJsonToObject(responseBody.getPayload(), MyCustomBean.class);
//call your business service class to do business with the received request.
} catch (Exception e) {
e.printStackTrace();
}
}
}

 

 

Feel free to comment for any doubts/feedbacks or suggestions.

856 total views, 4 views today

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.