ActiveMQ--模式(队列模式/主题模式)
两种模式:队列模式/主题模式
pom.xml <dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
队列模式,其实就是分食模式。
比如生产方发了 10条消息到 activeMQ 服务器, 而此时有多个 消费方,那么这些消费方就会瓜分这些10条消息,一条消息只会被一个消费方得到。
主题模式,就是订阅模式。
比如生产方发了10条消息,而此时有多个消费方,那么多个消费方都能得到这 10条消息,就如同订阅公众号那样。
队列模式:
1. 首先运行两次 TestConsumer 类,以启动两个不同的消费者
2. 运行一次 TestProducer, 以启动 生产者
生产者生产100个,两个消费者瓜分
消费者:
public class TestConsumer {
//服务地址,端口默认61616
private static final String url="tcp://127.0.0.1:61616";
//这次消费的消息名称
private static final String topicName="queue_style";
//消费者有可能是多个,为了区分不同的消费者,为其创建随机名称
private static final String consumerName="consumer-" + RandomUtil.randomString(5);
public static void main(String[] args) throws JMSException {
//0. 先判断端口是否启动了 Active MQ 服务器
ActiveMQUtil.checkServer();
System.out.printf("%s 消费者启动了。 %n", consumerName);
//1.创建ConnectiongFactory,绑定地址
ConnectionFactory factory=new ActiveMQConnectionFactory(url);
//2.创建Connection
Connection connection= factory.createConnection();
//3.启动连接
connection.start();
//4.创建会话
Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.创建一个目标 (主题类型)
Destination destination=session.createQueue(topicName);
//6.创建一个消费者
MessageConsumer consumer=session.createConsumer(destination);
//7.创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message arg0) {
// TODO Auto-generated method stub
TextMessage textMessage=(TextMessage)arg0;
try {
System.out.println(consumerName +" 接收消息:"+textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//8. 因为不知道什么时候有,所以没法主动关闭,就不关闭了,一直处于监听状态
//connection.close();
}
}
生产者:
public class TestProducer {
//服务地址,端口默认61616
private static final String url="tcp://127.0.0.1:61616";
//这次发送的消息名称
private static final String topicName="queue_style";
public static void main(String[] args) throws JMSException {
//0. 先判断端口是否启动了 Active MQ 服务器
ActiveMQUtil.checkServer();
//1.创建ConnectiongFactory,绑定地址
ConnectionFactory factory=new ActiveMQConnectionFactory(url);
//2.创建Connection
Connection connection= factory.createConnection();
//3.启动连接
connection.start();
//4.创建会话
Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//5.创建一个目标 (队列类型)
Destination destination=session.createQueue(topicName);
//6.创建一个生产者
MessageProducer producer=session.createProducer(destination);
for (int i = 0; i < 100; i++) {
//7.创建消息
TextMessage textMessage=session.createTextMessage("队列消息-"+i);
//8.发送消息
producer.send(textMessage);
System.out.println("发送:"+textMessage.getText());
}
//7. 关闭连接
connection.close();
}
}
2个consumer:

生产者生产:



主题模式:
消费者,生产者
public class TestConsumer {
//服务地址,端口默认61616
private static final String url="tcp://127.0.0.1:61616";
//这次消费的消息名称
private static final String topicName="topic_style";
//消费者有可能是多个,为了区分不同的消费者,为其创建随机名称
private static final String consumerName="consumer-" + RandomUtil.randomString(5);
public static void main(String[] args) throws JMSException {
//0. 先判断端口是否启动了 Active MQ 服务器
ActiveMQUtil.checkServer();
System.out.printf("%s 消费者启动了。 %n", consumerName);
//1.创建ConnectiongFactory,绑定地址
ConnectionFactory factory=new ActiveMQConnectionFactory(url);
//2.创建Connection
Connection connection= factory.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 arg0) {
// TODO Auto-generated method stub
TextMessage textMessage=(TextMessage)arg0;
try {
System.out.println(consumerName +" 接收消息:"+textMessage.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
//8. 因为不知道什么时候有,所以没法主动关闭,就不关闭了,一直处于监听状态
//connection.close();
}
}
public class TestProducer {
//服务地址,端口默认61616
private static final String url="tcp://127.0.0.1:61616";
//这次发送的消息名称
private static final String topicName="topic_style";
public static void main(String[] args) throws JMSException {
//0. 先判断端口是否启动了 Active MQ 服务器
ActiveMQUtil.checkServer();
//1.创建ConnectiongFactory,绑定地址
ConnectionFactory factory=new ActiveMQConnectionFactory(url);
//2.创建Connection
Connection connection= factory.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 < 100; i++) {
//7.创建消息
TextMessage textMessage=session.createTextMessage("主题消息-"+i);
//8.发送消息
producer.send(textMessage);
System.out.println("发送:"+textMessage.getText());
}
//7. 关闭连接
connection.close();
}
}
生产者生产100个,两个消费者都分别接受了100个




ActiveMQ--模式(队列模式/主题模式)的更多相关文章
- ActiveMQ队列、主题模式区别
1.ActiveMQ队列模式如下图,生产者创建消息到消息中间件,再“均分给消费者”. 2.ActiveMQ主题模式如下图,生产者创建消息到消息中间件,消费者会接受到订阅的主题中所有的消息.在主题模式下 ...
- activeMQ队列模式和主题模式的Java实现
一.队列模式 生产者 import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destina ...
- ActiveMQ的两种消息模式,主题、队列
1.开发的模式流程如下: 2.队列模式Queue 如果生产者产生了100条消息,那么两个消费同时在的话,会分工合作来接收这100条消息.就是每个消费者接收到50条来处理. 3.主题模式topic 如果 ...
- ActiveMQ基本详解与总结& 消息队列-推/拉模式学习 & ActiveMQ及JMS学习
转自:https://www.cnblogs.com/Survivalist/p/8094069.html ActiveMQ基本详解与总结 基本使用可以参考https://www.cnblogs.co ...
- 消息队列-推/拉模式学习 & ActiveMQ及JMS学习
一种分类是推和拉 . 还有一种分类是 Queue 和 Pub/Sub . 先看的这一篇:http://blog.csdn.net/heyutao007/article/details/50131089 ...
- RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)
前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...
- 队列模式&主题模式
# RabbitMQ 消息中间件 **Advanced Message Queuing Protocol (高级消息队列协议** The Advanced Message Queuing Protoc ...
- (八)RabbitMQ消息队列-通过Topic主题模式分发消息
原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...
- RabbitMQ六种队列模式-主题模式
前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...
随机推荐
- Travel in desert
传送门 不算难吧 应该有思路的 还是太水了吧 (而且和货车运输很像的啊 ---------------------------------------------------------------- ...
- 记录STM32调试
问题:加入红外初始化后,程序卡在红外初始化处 解决思路: 1.确认时钟是不是好的 2.把定时器分解调试(输入捕获.溢出分开一步一步弄) 已解决:定时器的溢出中断 注意:STM32Cube配置好后,需要 ...
- LAMP源码编译
---恢复内容开始--- 1.LAMP源码编译的基础环境 安装组建包:yum groupinstall "Development Tools" "Development ...
- tkinter+pickle+python的一个登录界面设计
1.代码: #导出模块 import tkinter as tk from tkinter import messagebox import pickle #定义登录的窗口.标题.大小和位置 wind ...
- IIS-代理
http://192.168.11.3:8083/java 访问 http://192.168.11.3:8089 http://192.168.11.3:8083/?id=1 访问http:/ ...
- DreamWeaver CC 中的回车
在Dreamweaver CC中换行有两种: 第一种是在设计视图中直接回车,对应的代码是<p>标签,即新生成一个段落. (注:在DreamWeaver CC编辑的代码中,按下回车相当于 ) ...
- Python 输入与输出
Python2版本 raw_input raw_input("输入提示"),会把输入的内容当做字符串返回 input 会把用户输入的内容当做代码来处理,可以理解为 raw_inpu ...
- openjudge(POJ)-1664 放苹果
对于n个盘子,m个苹果,我们要么在每个盘子上都放苹果,要么至少有一个盘子不放. 一个盘子不放就是f(m,n-1),全部都放的时候苹果就变成了n-m个,但是盘子的数目是不变的,因为此时还没有产生方案数, ...
- HDU1024 Max Sum Plus Plus (优化线性dp)
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
- 嵌入式实时程序设计中C/C++代码的优化
1 引言 计算机技术和信息技术的高速发展的今天,计算机和计算机技术大量应用在人们的日常生活中,嵌入式计算机也得到了广泛的应用.嵌入式计算机是指完成一种或多种特定功能的计算机系统,是软硬件的紧密结合体. ...