Skip to content

Commit 27f594b

Browse files
author
YunaiV
committed
增加 activemq 示例
1 parent 41cb36c commit 27f594b

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

lab-32/lab-32-activemq-native/pom.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
<artifactId>lab-32</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-32-activemq-native</artifactId>
13+
14+
<dependencies>
15+
<!-- 引入 ActiveMQ 客户端依赖 -->
16+
<dependency>
17+
<groupId>org.apache.activemq</groupId>
18+
<artifactId>activemq-client</artifactId>
19+
<version>5.15.10</version>
20+
</dependency>
21+
</dependencies>
22+
23+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package cn.iocoder.springboot.lab32.activemqdemo;
2+
3+
import javax.jms.*;
4+
import java.util.concurrent.TimeUnit;
5+
6+
public class ActiveMQConsumer {
7+
8+
public static void main(String[] args) throws JMSException {
9+
// 创建连接
10+
Connection connection = ActiveMQProducer.getConnection();
11+
12+
// 创建会话
13+
final Session session = ActiveMQProducer.getSession(connection);
14+
15+
// 创建队列
16+
Queue queue = ActiveMQProducer.getQueue(session);
17+
18+
// 创建 Consumer
19+
MessageConsumer consumer = session.createConsumer(queue);
20+
consumer.setMessageListener(new MessageListener() {
21+
22+
public void onMessage(Message message) {
23+
TextMessage textMessage = (TextMessage) message;
24+
try {
25+
System.out.println(String.format("[线程:%s][消息编号:%s][消息内容:%s]",
26+
Thread.currentThread(), textMessage.getJMSMessageID(), textMessage.getText()));
27+
} catch (JMSException e) {
28+
throw new RuntimeException(e);
29+
}
30+
}
31+
32+
});
33+
34+
// 关闭
35+
try {
36+
TimeUnit.HOURS.sleep(1);
37+
} catch (InterruptedException ignore) {
38+
}
39+
session.close();
40+
connection.close();
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cn.iocoder.springboot.lab32.activemqdemo;
2+
3+
import org.apache.activemq.ActiveMQConnectionFactory;
4+
5+
import javax.jms.*;
6+
7+
public class ActiveMQProducer {
8+
9+
private static final String BROKER_URL = "tcp://127.0.0.1:61616";
10+
private static final String USERNAME = "admin";
11+
private static final String PASSWORD = "admin";
12+
13+
private static final String QUEUE_NAME = "queue_demo"; // 只有 QUEUE_NAME 需要共享给 RabbitMQConsumer
14+
15+
public static void main(String[] args) throws JMSException {
16+
// 创建连接
17+
Connection connection = getConnection();
18+
19+
// 创建会话
20+
Session session = getSession(connection);
21+
22+
// 创建队列
23+
Queue queue = getQueue(session);
24+
25+
// 创建 Producer
26+
MessageProducer producer = session.createProducer(queue);
27+
28+
// 发送 3 条消息
29+
for (int i = 0; i < 3; i++) {
30+
Message message = session.createTextMessage("Hello World" + i);
31+
producer.send(message);
32+
}
33+
34+
// 关闭
35+
session.close();
36+
connection.close();
37+
}
38+
39+
public static Connection getConnection() throws JMSException {
40+
// 创建连接
41+
ConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);
42+
Connection connection = factory.createConnection();
43+
// 启动连接
44+
connection.start();
45+
return connection;
46+
}
47+
48+
public static Session getSession(Connection connection) throws JMSException {
49+
// 第一个方法参数 transacted ,是否开启事务。这里设置为 false ,无需开启
50+
// 第二个方法参数 acknowledgeMode ,确认模式。这里设置为 AUTO_ACKNOWLEDGE ,自动确认。推荐阅读 https://my.oschina.net/thinwonton/blog/995291
51+
return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
52+
}
53+
54+
public static Queue getQueue(Session session) throws JMSException {
55+
return session.createQueue(QUEUE_NAME);
56+
}
57+
58+
}

lab-32/pom.xml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
<artifactId>labs-parent</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>lab-32</artifactId>
13+
<packaging>pom</packaging>
14+
<modules>
15+
<module>lab-32-activemq-native</module>
16+
</modules>
17+
18+
19+
</project>

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<module>lab-29</module>
4141
<module>lab-30</module>
4242
<module>lab-31</module>
43+
<module>lab-32</module>
4344
</modules>
4445

4546

0 commit comments

Comments
 (0)