mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 activemq 示例
This commit is contained in:
23
lab-32/lab-32-activemq-native/pom.xml
Normal file
23
lab-32/lab-32-activemq-native/pom.xml
Normal file
@@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>lab-32</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-32-activemq-native</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 ActiveMQ 客户端依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.activemq</groupId>
|
||||
<artifactId>activemq-client</artifactId>
|
||||
<version>5.15.10</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
19
lab-32/pom.xml
Normal file
19
lab-32/pom.xml
Normal file
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labs-parent</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-32</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-32-activemq-native</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user