From 27f594b491d623b455abd188ea4d9a72205cbbf3 Mon Sep 17 00:00:00 2001
From: YunaiV <>
Date: Sat, 14 Dec 2019 23:26:25 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20activemq=20=E7=A4=BA?=
=?UTF-8?q?=E4=BE=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
lab-32/lab-32-activemq-native/pom.xml | 23 ++++++++
.../lab32/activemqdemo/ActiveMQConsumer.java | 43 ++++++++++++++
.../lab32/activemqdemo/ActiveMQProducer.java | 58 +++++++++++++++++++
lab-32/pom.xml | 19 ++++++
pom.xml | 1 +
5 files changed, 144 insertions(+)
create mode 100644 lab-32/lab-32-activemq-native/pom.xml
create mode 100644 lab-32/lab-32-activemq-native/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/ActiveMQConsumer.java
create mode 100644 lab-32/lab-32-activemq-native/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/ActiveMQProducer.java
create mode 100644 lab-32/pom.xml
diff --git a/lab-32/lab-32-activemq-native/pom.xml b/lab-32/lab-32-activemq-native/pom.xml
new file mode 100644
index 00000000..4095e9c5
--- /dev/null
+++ b/lab-32/lab-32-activemq-native/pom.xml
@@ -0,0 +1,23 @@
+
+
+
+ lab-32
+ cn.iocoder.springboot.labs
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ lab-32-activemq-native
+
+
+
+
+ org.apache.activemq
+ activemq-client
+ 5.15.10
+
+
+
+
diff --git a/lab-32/lab-32-activemq-native/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/ActiveMQConsumer.java b/lab-32/lab-32-activemq-native/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/ActiveMQConsumer.java
new file mode 100644
index 00000000..e8a36ed3
--- /dev/null
+++ b/lab-32/lab-32-activemq-native/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/ActiveMQConsumer.java
@@ -0,0 +1,43 @@
+package cn.iocoder.springboot.lab32.activemqdemo;
+
+import javax.jms.*;
+import java.util.concurrent.TimeUnit;
+
+public class ActiveMQConsumer {
+
+ public static void main(String[] args) throws JMSException {
+ // 创建连接
+ Connection connection = ActiveMQProducer.getConnection();
+
+ // 创建会话
+ final Session session = ActiveMQProducer.getSession(connection);
+
+ // 创建队列
+ Queue queue = ActiveMQProducer.getQueue(session);
+
+ // 创建 Consumer
+ MessageConsumer consumer = session.createConsumer(queue);
+ consumer.setMessageListener(new MessageListener() {
+
+ public void onMessage(Message message) {
+ TextMessage textMessage = (TextMessage) message;
+ try {
+ System.out.println(String.format("[线程:%s][消息编号:%s][消息内容:%s]",
+ Thread.currentThread(), textMessage.getJMSMessageID(), textMessage.getText()));
+ } catch (JMSException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ });
+
+ // 关闭
+ try {
+ TimeUnit.HOURS.sleep(1);
+ } catch (InterruptedException ignore) {
+ }
+ session.close();
+ connection.close();
+ }
+
+}
diff --git a/lab-32/lab-32-activemq-native/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/ActiveMQProducer.java b/lab-32/lab-32-activemq-native/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/ActiveMQProducer.java
new file mode 100644
index 00000000..c8997b58
--- /dev/null
+++ b/lab-32/lab-32-activemq-native/src/main/java/cn/iocoder/springboot/lab32/activemqdemo/ActiveMQProducer.java
@@ -0,0 +1,58 @@
+package cn.iocoder.springboot.lab32.activemqdemo;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+
+import javax.jms.*;
+
+public class ActiveMQProducer {
+
+ private static final String BROKER_URL = "tcp://127.0.0.1:61616";
+ private static final String USERNAME = "admin";
+ private static final String PASSWORD = "admin";
+
+ private static final String QUEUE_NAME = "queue_demo"; // 只有 QUEUE_NAME 需要共享给 RabbitMQConsumer
+
+ public static void main(String[] args) throws JMSException {
+ // 创建连接
+ Connection connection = getConnection();
+
+ // 创建会话
+ Session session = getSession(connection);
+
+ // 创建队列
+ Queue queue = getQueue(session);
+
+ // 创建 Producer
+ MessageProducer producer = session.createProducer(queue);
+
+ // 发送 3 条消息
+ for (int i = 0; i < 3; i++) {
+ Message message = session.createTextMessage("Hello World" + i);
+ producer.send(message);
+ }
+
+ // 关闭
+ session.close();
+ connection.close();
+ }
+
+ public static Connection getConnection() throws JMSException {
+ // 创建连接
+ ConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);
+ Connection connection = factory.createConnection();
+ // 启动连接
+ connection.start();
+ return connection;
+ }
+
+ public static Session getSession(Connection connection) throws JMSException {
+ // 第一个方法参数 transacted ,是否开启事务。这里设置为 false ,无需开启
+ // 第二个方法参数 acknowledgeMode ,确认模式。这里设置为 AUTO_ACKNOWLEDGE ,自动确认。推荐阅读 https://my.oschina.net/thinwonton/blog/995291
+ return connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ }
+
+ public static Queue getQueue(Session session) throws JMSException {
+ return session.createQueue(QUEUE_NAME);
+ }
+
+}
diff --git a/lab-32/pom.xml b/lab-32/pom.xml
new file mode 100644
index 00000000..64e44541
--- /dev/null
+++ b/lab-32/pom.xml
@@ -0,0 +1,19 @@
+
+
+
+ labs-parent
+ cn.iocoder.springboot.labs
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ lab-32
+ pom
+
+ lab-32-activemq-native
+
+
+
+
diff --git a/pom.xml b/pom.xml
index e72ca344..afc0deb9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -40,6 +40,7 @@
lab-29
lab-30
lab-31
+ lab-32