主题模式 又名 发布订阅者模式(Pub/Sub)。
一、主题模式特点
客户端包括发布者和订阅者
主题中的消息被所有订阅者消费
消费者不能消费订阅之前就发送到主题中的消息
二、创建过程
1.创建连接Connection
2.创建会话Session
3.通过Session来创建其它的(MessageProducer、MessageConsumer、Destination、TextMessage)
4.将生产者 MessageProducer 和消费者 MessageConsumer 都会指向目标 Destination
5.生产者向目标发送TextMessage消息send()
6.消费者设置监听器,监听消息。
三、代码实现
相对于前一篇文章所讲的队列模式,主题模式只需要修改其中的createQueue()为createTopic();
1. 创建Maven项目
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">
4.0.0
com.jms
jms-test
1.0-SNAPSHOT
org.apache.activemq
activemq-all
5.9.0
2. 生产者 AppProducer.java
public class AppProducer {
private static final String url = "tcp://127.0.0.1:61616";
private static final String topicName = "topic-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.createTopic(topicName);
//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();
}
}
3. 消费者 AppConsumer.java
public class AppConsumer {
private static final String url = "tcp://127.0.0.1:61616";
private static final String topicName = "topic-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.createTopic(topicName);
//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();
}
}
四、运行查看
1. 先运行生产者 AppProducer
会发现有10条消息被发布
2. 消息发布后再开启一个消费者 AppConsumer
运行AppConsumer.java后会发现发布的10条消息并没有被消费者接收,因为在主题模式中: 只有提前进行订阅的消费者才能成功消费消息。而队列模式中消费者不需要提前订阅也可以消费消息。如下图:
3.先开启两个消费者,后运行生产者
会发现生产者发送的10个消息,两个消费者都全部接收。
AppConsumer1
接收消息 = [test0]
接收消息 = [test1]
接收消息 = [test2]
接收消息 = [test3]
接收消息 = [test4]
接收消息 = [test5]
接收消息 = [test6]
接收消息 = [test7]
接收消息 = [test8]
接收消息 = [test9]
AppConsumer2
接收消息 = [test0]
接收消息 = [test1]
接收消息 = [test2]
接收消息 = [test3]
接收消息 = [test4]
接收消息 = [test5]
接收消息 = [test6]
接收消息 = [test7]
接收消息 = [test8]
接收消息 = [test9]
4. 小结
先启动生产者,发布10条消息,然后再启动消费者,这时消费者是不能消费到消息的,因为主题模式中: 只有提前进行订阅的消费者才能成功消费消息。而队列模式消费者不需要提前订阅也可以消费消息
先启动一个消费者,然后再启动生产者发布10条消息,这时消费者成功消费了ActiveMQ服务器中的消息。
先启动两个消费者,然后启动生产者发布10条消息,这时两个消费者都可以消费ActiveMQ服务器中的每一条消息。这就是主题模式的特点: 每个订阅者都可以消费主题模式中的每一条消息。而队列模式中,只能平均消费消息,被别的消费者消费的消息不能重复被其他的消费者消费
五、队列模式和主题模式的区别
是否需要提前订阅
队列模式:消费者不需要提前订阅也可以消费消息
主题模式:只有提前进行订阅的消费者才能成功消费消息
多个消费者如何分配消息
队列模式:只能平均消费消息,被别的消费者消费的消息不能重复被其他的消费者消费
主题模式:每个订阅者都可以消费主题模式中的每一条消息