Skip to content

Commit 939c5aa

Browse files
committed
Import: rabbitmq-sender project
1 parent 0ed789c commit 939c5aa

File tree

6 files changed

+130
-36
lines changed

6 files changed

+130
-36
lines changed

asset-manager/.gitignore

Lines changed: 0 additions & 35 deletions
This file was deleted.

mi-sql-public-demo/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

rabbitmq-sender/pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
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+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.3.0</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>messaging-rabbitmq</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>messaging-rabbitmq</name>
15+
<description>Demo project for rabbitmq</description>
16+
<properties>
17+
<java.version>17</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-amqp</artifactId>
23+
</dependency>
24+
25+
<!--
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-docker-compose</artifactId>
29+
<scope>runtime</scope>
30+
<optional>true</optional>
31+
</dependency>
32+
-->
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<scope>provided</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<version>1.18.24</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>com.fasterxml.jackson.core</groupId>
52+
<artifactId>jackson-databind</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.springframework.boot</groupId>
60+
<artifactId>spring-boot-maven-plugin</artifactId>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.messagingrabbitmq;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.ConfigurableApplicationContext;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.amqp.core.Queue;
8+
9+
@SpringBootApplication
10+
public class MessagingRabbitmqApplication {
11+
12+
static final String queueName1 = "queue1";
13+
static final String queueName2 = "queue2";
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
ConfigurableApplicationContext applicationContext = SpringApplication.run(MessagingRabbitmqApplication.class);
17+
Producer producer = applicationContext.getBean(Producer.class);
18+
producer.run();
19+
}
20+
21+
@Bean
22+
public Queue queue1() {
23+
return new Queue(queueName1, true);
24+
}
25+
26+
@Bean
27+
public Queue queue2() {
28+
return new Queue(queueName2, true);
29+
}
30+
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.messagingrabbitmq;
2+
3+
import org.springframework.amqp.core.Message;
4+
import org.springframework.stereotype.Component;
5+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
8+
@Component
9+
public class Producer {
10+
11+
@Autowired
12+
private final RabbitTemplate rabbitTemplate;
13+
14+
public Producer(RabbitTemplate rabbitTemplate) {
15+
this.rabbitTemplate = rabbitTemplate;
16+
}
17+
18+
public void run() {
19+
for (int i = 0; i < 10; i++) {
20+
System.out.println("Sending message..."+i);
21+
String responseString = "test "+i;
22+
Message responseMessage = new Message(responseString.getBytes());
23+
if (i % 2 == 0) {
24+
rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.queueName2, responseMessage);
25+
} else {
26+
rabbitTemplate.convertAndSend(MessagingRabbitmqApplication.queueName1, responseMessage);
27+
}
28+
}
29+
}
30+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.rabbitmq.password=secret
2+
spring.rabbitmq.username=myuser
3+
spring.rabbitmq.host=localhost
4+
spring.rabbitmq.port=5671

0 commit comments

Comments
 (0)