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
-
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:
xxxxxxxxxx
1host: localhost
2port: 5672
3username: guest
4password: guest
-
Project structure:
DemoApplication.java
file:
xxxxxxxxxx
1
package com.example.demo;
2
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6
7
public class DemoApplication {
8
9
public static void main(String[] args) {
10
SpringApplication.run(DemoApplication.class, args);
11
}
12
}
ProducerConfig.java
file:
xxxxxxxxxx
1
package com.example.demo;
2
3
import org.springframework.amqp.core.Queue;
4
import org.springframework.context.annotation.Bean;
5
import org.springframework.context.annotation.Configuration;
6
7
8
public class ProducerConfig {
9
10
11
public Queue myQueue() { // creates my-queue automatically on the RabbitMQ server if not available
12
return new Queue("my-queue");
13
}
14
}
ProducerController.java
file:
xxxxxxxxxx
1
package com.example.demo;
2
3
import org.springframework.beans.factory.annotation.Autowired;
4
import org.springframework.stereotype.Controller;
5
import org.springframework.web.bind.annotation.RequestMapping;
6
import org.springframework.web.bind.annotation.ResponseBody;
7
8
9
public class ProducerController {
10
11
12
private ProducerService producerService;
13
14
"/test") (
15
16
public void test() {
17
this.producerService.sendMessage("Testing message...");
18
}
19
}
ProducerService.java
file:
xxxxxxxxxx
1
package com.example.demo;
2
3
import org.springframework.amqp.rabbit.core.RabbitTemplate;
4
import org.springframework.beans.factory.annotation.Autowired;
5
import org.springframework.stereotype.Service;
6
7
8
public class ProducerService {
9
10
11
private RabbitTemplate template;
12
13
public void sendMessage(String message) {
14
this.template.convertAndSend("my-queue", message); // sends string message to my-queue
15
}
16
}
application.properties
file:
xxxxxxxxxx
1
server.port=8081
2
3
spring.rabbitmq.host=localhost
4
spring.rabbitmq.port=5672
5
spring.rabbitmq.username=guest
6
spring.rabbitmq.password=guest
Project structure:
DemoApplication.java
file:
xxxxxxxxxx
1
package com.example.demo;
2
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
6
7
public class DemoApplication {
8
9
public static void main(String[] args) {
10
SpringApplication.run(DemoApplication.class, args);
11
}
12
}
ConsumerConfig.java
file:
xxxxxxxxxx
1
package com.example.demo;
2
3
import org.springframework.amqp.core.Queue;
4
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
5
import org.springframework.context.annotation.Bean;
6
import org.springframework.context.annotation.Configuration;
7
8
9
10
public class ConsumerConfig {
11
12
13
public Queue myQueue() { // creates my-queue automatically on the RabbitMQ server if not available
14
return new Queue("my-queue");
15
}
16
}
ConsumerService.java
file:
xxxxxxxxxx
1
package com.example.demo;
2
3
import org.springframework.amqp.rabbit.annotation.RabbitListener;
4
import org.springframework.stereotype.Service;
5
6
7
public class ConsumerService {
8
9
queues = "my-queue") (
10
public void handleMyQueue(String message) { // receives string message from my-queue
11
System.out.println("Message: " + message);
12
}
13
}
application.properties
file:
xxxxxxxxxx
1
server.port=8082
2
3
spring.rabbitmq.host=localhost
4
spring.rabbitmq.port=5672
5
spring.rabbitmq.username=guest
6
spring.rabbitmq.password=guest
pom.xml
file:
xxxxxxxxxx
1
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
5
<modelVersion>4.0.0</modelVersion>
6
7
<parent>
8
<groupId>org.springframework.boot</groupId>
9
<artifactId>spring-boot-starter-parent</artifactId>
10
<version>2.7.0</version>
11
<relativePath/> <!-- lookup parent from repository -->
12
</parent>
13
14
<groupId>com.example</groupId>
15
<artifactId>demo</artifactId>
16
<version>0.0.1-SNAPSHOT</version>
17
18
<name>demo</name>
19
<description>Demo project for Spring Boot</description>
20
21
<properties>
22
<java.version>11</java.version>
23
</properties>
24
25
<dependencies>
26
<dependency>
27
<groupId>org.springframework.boot</groupId>
28
<artifactId>spring-boot-starter-web</artifactId>
29
</dependency>
30
<dependency> <!-- --- required -->
31
<groupId>org.springframework.boot</groupId> <!-- --- required -->
32
<artifactId>spring-boot-starter-amqp</artifactId> <!-- --- required -->
33
</dependency> <!-- --- required -->
34
<dependency>
35
<groupId>org.springframework.boot</groupId>
36
<artifactId>spring-boot-starter-test</artifactId>
37
<scope>test</scope>
38
</dependency>
39
<dependency> <!-- --- required for tests only -->
40
<groupId>org.springframework.amqp</groupId> <!-- --- required for tests only -->
41
<artifactId>spring-rabbit-test</artifactId> <!-- --- required for tests only -->
42
<scope>test</scope> <!-- --- required for tests only -->
43
</dependency> <!-- --- required for tests only -->
44
</dependencies>
45
46
<build>
47
<plugins>
48
<plugin>
49
<groupId>org.springframework.boot</groupId>
50
<artifactId>spring-boot-maven-plugin</artifactId>
51
</plugin>
52
</plugins>
53
</build>
54
55
</project>