EN
Spring Boot 2 - RabbitMQ simple example (producer + consumer)
3
points
In this article, we would like to show you how to create a simple project that uses RabbitMQ to transfer messages from one application to another using Spring Boot 2.
Definitions:
- producer - the logic that sends messages
- consumer - the logic that receives messages
Requirements
-
RabbitMQ
-
Installation as service: RabbitMQ - service installation under Windows
-
Installation with Docker: Rabbitmq - Official Image | Docker Hub
Hint:
Default configuration to get connection with RabbitMQ is:
host: localhost port: 5672 username: guest password: guest
-
Spring Boot 2 Applications
Producer application example
Project structure:
DemoApplication.java
file:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
ProducerConfig.java
file:
package com.example.demo;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ProducerConfig {
@Bean
public Queue myQueue() { // creates my-queue automatically on the RabbitMQ server if not available
return new Queue("my-queue");
}
}
ProducerController.java
file:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ProducerController {
@Autowired
private ProducerService producerService;
@RequestMapping("/test")
@ResponseBody
public void test() {
this.producerService.sendMessage("Testing message...");
}
}
ProducerService.java
file:
package com.example.demo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ProducerService {
@Autowired
private RabbitTemplate template;
public void sendMessage(String message) {
this.template.convertAndSend("my-queue", message); // sends string message to my-queue
}
}
application.properties
file:
server.port=8081
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
Consumer application example
Project structure:
DemoApplication.java
file:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
ConsumerConfig.java
file:
package com.example.demo;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableRabbit
public class ConsumerConfig {
@Bean
public Queue myQueue() { // creates my-queue automatically on the RabbitMQ server if not available
return new Queue("my-queue");
}
}
ConsumerService.java
file:
package com.example.demo;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class ConsumerService {
@RabbitListener(queues = "my-queue")
public void handleMyQueue(String message) { // receives string message from my-queue
System.out.println("Message: " + message);
}
}
application.properties
file:
server.port=8082
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
Common files
pom.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency> <!-- --- required -->
<groupId>org.springframework.boot</groupId> <!-- --- required -->
<artifactId>spring-boot-starter-amqp</artifactId> <!-- --- required -->
</dependency> <!-- --- required -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency> <!-- --- required for tests only -->
<groupId>org.springframework.amqp</groupId> <!-- --- required for tests only -->
<artifactId>spring-rabbit-test</artifactId> <!-- --- required for tests only -->
<scope>test</scope> <!-- --- required for tests only -->
</dependency> <!-- --- required for tests only -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>