How to Use RabbitMQ with Spring Boot: Simple Guide
To use
RabbitMQ with Spring Boot, add the spring-boot-starter-amqp dependency, configure connection settings in application.properties, and create message sender and listener components using RabbitTemplate and @RabbitListener. This setup enables sending and receiving messages asynchronously with minimal code.Syntax
To integrate RabbitMQ with Spring Boot, you need to:
- Add the
spring-boot-starter-amqpdependency to your project. - Configure RabbitMQ connection properties in
application.propertiesorapplication.yml. - Create a
RabbitTemplatebean to send messages. - Use
@RabbitListenerannotation on methods to receive messages asynchronously.
java / properties
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-amqp'
}
# application.properties
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
// Sending message
@Autowired
private RabbitTemplate rabbitTemplate;
rabbitTemplate.convertAndSend("exchangeName", "routingKey", "message");
// Receiving message
@RabbitListener(queues = "queueName")
public void receiveMessage(String message) {
System.out.println("Received: " + message);
}Example
This example shows a Spring Boot application that sends a message to a RabbitMQ queue and listens for messages from that queue.
java
package com.example.rabbitmqdemo; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.annotation.EnableRabbit; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableRabbit public class RabbitmqDemoApplication implements CommandLineRunner { @Autowired private RabbitTemplate rabbitTemplate; public static void main(String[] args) { SpringApplication.run(RabbitmqDemoApplication.class, args); } @Bean public Queue myQueue() { return new Queue("myQueue", false); } @Override public void run(String... args) throws Exception { System.out.println("Sending message..."); rabbitTemplate.convertAndSend("myQueue", "", "Hello RabbitMQ!"); } @RabbitListener(queues = "myQueue") public void listen(String message) { System.out.println("Received message: " + message); } }
Output
Sending message...
Received message: Hello RabbitMQ!
Common Pitfalls
- Missing dependency: Forgetting to add
spring-boot-starter-amqpcauses connection failures. - Incorrect RabbitMQ settings: Wrong host, port, or credentials prevent connection.
- Queue not declared: Sending or listening to a queue that does not exist causes errors; declare queues as beans or in RabbitMQ.
- Listener method signature: Listener methods must match the message type; otherwise, messages won't be processed.
java
/* Wrong: No queue declared, sending to non-existing queue */ rabbitTemplate.convertAndSend("nonExistingQueue", "Test"); /* Right: Declare queue bean and send to it */ @Bean public Queue myQueue() { return new Queue("myQueue", false); } rabbitTemplate.convertAndSend("myQueue", "Test");
Quick Reference
Remember these key points when using RabbitMQ with Spring Boot:
- Add
spring-boot-starter-amqpdependency. - Configure connection in
application.properties. - Declare queues as beans or ensure they exist in RabbitMQ.
- Use
RabbitTemplateto send messages. - Use
@RabbitListenerto receive messages asynchronously.
Key Takeaways
Add spring-boot-starter-amqp dependency to enable RabbitMQ support in Spring Boot.
Configure RabbitMQ connection details in application.properties for proper connectivity.
Declare queues explicitly to avoid runtime errors when sending or receiving messages.
Use RabbitTemplate to send messages and @RabbitListener to receive them asynchronously.
Ensure listener method signatures match the expected message type for correct processing.