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.
Table of Contents
Prerequisite:
[xml]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
[/xml]
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.
[java]
@Service (or) @Component
public class Sender {
// yourqueue.simple is referred from application.properties
@Value("${yourqueue.simple}")
String myQueuName;
@Autowired
private RabbitTemplate rabbitTemplate;
public String sendMessage(String request){
rabbitTemplate.convertAndSend(queue, request);
}
}
[/java]
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.
[java]
@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();
}
}
}
[/java]
Feel free to comment for any doubts/feedbacks or suggestions.