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六种队列模式-主题模式 [ ...
随机推荐
- 关于AD元件的命名
1.电容 C? 10uf 2.电阻 R? 10k 3.芯片 U? STM32F103VET6 4.单排 J? SIP 5.三极管 Q? s8550 6.晶振 Y? 12M
- 如何切换虚拟机(centos6)和windows
通过设置热键,选择Ctrl+Alt+Fx即可.重启linux之后按Ctrl+Alt+Fx切换不同的终端的就可以了 图一. 图二.
- 1143. Longest Common Subsequence
link to problem Description: Given two strings text1 and text2, return the length of their longest c ...
- selenium+python+unittest实现自动化测试(入门篇)
本文主要讲解关于selenium自动化测试框架的入门知识点,教大家如何搭建selenium自动化测试环境,如何用selenium+python+unittest实现web页面的自动化测试,先来看看se ...
- Vacuum Pump Manufacturer - Vacuum Pump: Prevents Reactive Compound Decomposition Products
Vacuum packaging has been popular in the industry for a long time. Many large companies have joined ...
- Java面向对象简单知识总结-考试用
类.对象.构造器 类定义了属性.方法,是抽象的,写在扩展名为java的文件中. 对象是类的实体,是具体的. 构造器:方法名与类名一致.没有返回类型,可以重载不能重写.在创建对象时调用.使用new调用实 ...
- cf--TV Subscriptions (Hard Version)
time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standa ...
- 吴裕雄--天生自然Numpy库学习笔记:NumPy 线性代数
import numpy.matlib import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[11,12],[13,14]]) p ...
- 重新梳理IT知识之java-03循环
引用变量时要给变量赋值,如果循环进不去就会报错. 一.循环结构的四要素 1.初始化条件 2.循环条件 ---> 是Boolean类型 3.循环体 4.迭代条件 说明:通常情况下,循环结束都是因为 ...
- stm32CubeMx lwip + freeRTOS
MCU: STM32F429IGT6 工具:STM32CubeMx 版本号 5.0.0 Keil uVersion5 目的:使用LWIP 实现简单的网络连通 一 简介 LWIP(Light Wei ...