Skip to content

Commit 1b8cd82

Browse files
author
YunaiV
committed
增加 activemq 示例
1 parent 027b082 commit 1b8cd82

File tree

19 files changed

+160
-0
lines changed

19 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-32-activemq-demo-message-model</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对 ActiveMQ 的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-activemq</artifactId>
20+
</dependency>
21+
22+
<!-- 方便等会写单元测试 -->
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-test</artifactId>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab32.activemqdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
spring:
2+
# ActiveMQ 配置项,对应 ActiveMQProperties 配置类
3+
activemq:
4+
broker-url: tcp://127.0.0.1:61616 # RabbitMQ Broker 的地址
5+
user: admin # 账号
6+
password: admin # 密码
7+
packages:
8+
trust-all: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cn.iocoder.springboot.lab32.activemqdemo;

lab-32/lab-32-activemq-demo/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/Application.java

+2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableAsync;
56

67
@SpringBootApplication
8+
@EnableAsync // 开启异步
79
public class Application {
810

911
public static void main(String[] args) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.iocoder.springboot.lab32.activemqdemo.consumer;
2+
3+
import cn.iocoder.springboot.lab32.activemqdemo.message.Demo01Message;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class Demo01Consumer {
10+
11+
private Logger logger = LoggerFactory.getLogger(getClass());
12+
13+
// @JmsListener(destination = Demo01Message.QUEUE)
14+
public void onMessage(Demo01Message message) {
15+
logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
16+
}
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.iocoder.springboot.lab32.activemqdemo.message;
2+
3+
import java.io.Serializable;
4+
5+
public class Demo01Message implements Serializable {
6+
7+
public static final String QUEUE = "QUEUE_DEMO_01";
8+
9+
/**
10+
* 编号
11+
*/
12+
private Integer id;
13+
14+
public Demo01Message setId(Integer id) {
15+
this.id = id;
16+
return this;
17+
}
18+
19+
public Integer getId() {
20+
return id;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "ClusteringtMessage{" +
26+
"id=" + id +
27+
'}';
28+
}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cn.iocoder.springboot.lab32.activemqdemo.producer;
2+
3+
import cn.iocoder.springboot.lab32.activemqdemo.message.Demo01Message;
4+
import org.springframework.jms.core.JmsMessagingTemplate;
5+
import org.springframework.stereotype.Component;
6+
7+
import javax.annotation.Resource;
8+
9+
@Component
10+
public class Demo01Producer {
11+
12+
@Resource
13+
private JmsMessagingTemplate rabbitTemplate;
14+
15+
public void syncSend(Integer id) {
16+
// 创建 ClusteringMessage 消息
17+
Demo01Message message = new Demo01Message();
18+
message.setId(id);
19+
// 同步发送消息
20+
rabbitTemplate.convertAndSend(Demo01Message.QUEUE, message);
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.iocoder.springboot.lab32.activemqdemo.producer;
2+
3+
import cn.iocoder.springboot.lab32.activemqdemo.Application;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
import java.util.concurrent.CountDownLatch;
13+
14+
@RunWith(SpringRunner.class)
15+
@SpringBootTest(classes = Application.class)
16+
public class Demo01ProducerTest {
17+
18+
private Logger logger = LoggerFactory.getLogger(getClass());
19+
20+
@Autowired
21+
private Demo01Producer producer;
22+
23+
@Test
24+
public void testSyncSend() throws InterruptedException {
25+
// 发送消息
26+
int id = (int) (System.currentTimeMillis() / 1000);
27+
producer.syncSend(id);
28+
logger.info("[testSyncSend][发送编号:[{}] 发送成功]", id);
29+
30+
// 阻塞等待,保证消费
31+
new CountDownLatch(1).await();
32+
}
33+
34+
}

lab-32/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<modules>
1515
<module>lab-32-activemq-native</module>
1616
<module>lab-32-activemq-demo</module>
17+
<module>lab-32-activemq-demo-message-model</module>
1718
</modules>
1819

1920

0 commit comments

Comments
 (0)