标签:mamicode http 订阅 重复 代码 font art 连接 nbsp
此处的代码只是简化理解,实际项目会结合Spring使用。
1.创建连接Connection
2.创建会话Session
3.通过Session来创建其它的(MessageProducer、MessageConsumer、Destination、TextMessage)
4.将生产者 MessageProducer 和消费者 MessageConsumer 都会指向目标 Destination
5.生产者向目标发送TextMessage消息send()
6.消费者设置监听器,监听消息。
<?xml version="1.0" encoding="UTF-8"?>
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jms</groupId>
<artifactId>jms-test</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- activemq依赖 -->
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
</dependencies>
</project>
public class AppProducer {
private static final String url = "tcp://127.0.0.1:61616";
private static final String queueName = "queue-test";
public static void main(String[] args) throws JMSException {
//1.创建ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//2.创建Connection
Connection connection = connectionFactory.createConnection();
//3.启动连接
connection.start();
//4.创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.创建一个目标
Destination destination = session.createQueue(queueName);
//6.创建一个生产者
MessageProducer producer = session.createProducer(destination);
for (int i = 0; i < 10; i++) {
//7.创建消息
TextMessage textMessage = session.createTextMessage("test" + i);
//8.发布消息
producer.send(textMessage);
System.out.println("发送消息"+textMessage.getText());
}
//9.关闭连接
connection.close();
}
}
消费者的连接Connection是不能关闭的,因为消息的接收是异步的,会导致消息不能被消费。
public class AppConsumer {
private static final String url = "tcp://127.0.0.1:61616";
private static final String queueName = "queue-test";
public static void main(String[] args) throws JMSException {
//1. 创建ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
//2. 创建Connection
Connection connection = connectionFactory.createConnection();
//3. 启动连接
connection.start();
//4. 创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5. 创建一个目标
Destination destination = session.createQueue(queueName);
//6. 创建一个消费者
MessageConsumer consumer = session.createConsumer(destination);
//7. 创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
System.out.println("接收消息 = [" + ((TextMessage) message).getText() + "]");
} catch (JMSException e) {
e.printStackTrace();
}
}
});
//8.关闭连接(消费者的连接不允许关闭的,因为消息的接收是异步的,会导致消息不能被消费)
//connection.close();
}
}
运行AppProducer.java后会发现队列中添加了10条消息,如下图:
运行AppConsumer.java后会发现队列中的10条消息被消费了,如下图:
会发现生产者发送的10个消息,被两个消费者平分了。
AppConsumer1
接收消息 = [test1]
接收消息 = [test3]
接收消息 = [test5]
接收消息 = [test7]
接收消息 = [test9]
AppConsumer2
接收消息 = [test0]
接收消息 = [test2]
接收消息 = [test4]
接收消息 = [test6]
接收消息 = [test8]
标签:mamicode http 订阅 重复 代码 font art 连接 nbsp
原文地址:https://www.cnblogs.com/telwanggs/p/13183529.html