Using RabbitMQ in Spring Boot is very easy. Spring Boot provides the spring-boot-starter-amqp dependency that integrates Spring AMQP and RabbitMQ. Here is a simple example that shows how to configure and use RabbitMQ in a Spring Boot application.
Add the following dependencies in your pom.xml/build.gradle file
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-amqp'
}
Configure RabbitMQ connection
application.yml
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
Create a configuration class
java
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
public static final String QUEUE_NAME = "hello";
public static final String EXCHANGE_NAME = "hello-exchange";
@Bean
public Queue queue() {
return new Queue(QUEUE_NAME, false);
}
@Bean
public TopicExchange exchange() {
return new TopicExchange(EXCHANGE_NAME);
}
@Bean
public Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("hello.#");
}
}
Create a producer
java
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RabbitMQSender {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(String message) {
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "hello.routingkey", message);
System.out.println(" [x] Sent '" + message + "'");
}
}
Create a consumer
java
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class RabbitMQReceiver {
@RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
public void receive(String message) {
System.out.println(" [x] Received '" + message + "'");
}
}
Create a simple controller
java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RabbitMQController {
@Autowired
private RabbitMQSender rabbitMQSender;
@GetMapping("/send")
public String send(@RequestParam String message) {
rabbitMQSender.send(message);
return "Message sent: " + message;
}
}
Run the application
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RabbitMQApplication {
public static void main(String[] args) {
SpringApplication.run(RabbitMQApplication.class, args);
}
}