两种模式:队列模式/主题模式

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--模式(队列模式/主题模式)的更多相关文章

  1. ActiveMQ队列、主题模式区别

    1.ActiveMQ队列模式如下图,生产者创建消息到消息中间件,再“均分给消费者”. 2.ActiveMQ主题模式如下图,生产者创建消息到消息中间件,消费者会接受到订阅的主题中所有的消息.在主题模式下 ...

  2. activeMQ队列模式和主题模式的Java实现

    一.队列模式 生产者 import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.Destina ...

  3. ActiveMQ的两种消息模式,主题、队列

    1.开发的模式流程如下: 2.队列模式Queue 如果生产者产生了100条消息,那么两个消费同时在的话,会分工合作来接收这100条消息.就是每个消费者接收到50条来处理. 3.主题模式topic 如果 ...

  4. ActiveMQ基本详解与总结& 消息队列-推/拉模式学习 & ActiveMQ及JMS学习

    转自:https://www.cnblogs.com/Survivalist/p/8094069.html ActiveMQ基本详解与总结 基本使用可以参考https://www.cnblogs.co ...

  5. 消息队列-推/拉模式学习 & ActiveMQ及JMS学习

    一种分类是推和拉 . 还有一种分类是 Queue 和 Pub/Sub . 先看的这一篇:http://blog.csdn.net/heyutao007/article/details/50131089 ...

  6. RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)

    前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...

  7. 队列模式&主题模式

    # RabbitMQ 消息中间件 **Advanced Message Queuing Protocol (高级消息队列协议** The Advanced Message Queuing Protoc ...

  8. (八)RabbitMQ消息队列-通过Topic主题模式分发消息

    原文:(八)RabbitMQ消息队列-通过Topic主题模式分发消息 前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过 ...

  9. RabbitMQ六种队列模式-主题模式

    前言 RabbitMQ六种队列模式-简单队列RabbitMQ六种队列模式-工作队列RabbitMQ六种队列模式-发布订阅RabbitMQ六种队列模式-路由模式RabbitMQ六种队列模式-主题模式 [ ...

随机推荐

  1. National Contest for Private Universities (NCPU), 2019 C Boxes(双向链表)

    题目中的要求如果x在y的左边,不需要移动,x在y的右边,2操作不需要移动. 有一个问题是,如果x与y相邻,这时的swap操作变成了三个而不是四个,这点尤其需要注意,不然就会死循环.注意x是和y相邻,这 ...

  2. Jmeter变量嵌套的方法

    jmeter中变量的嵌套一般有两种方式 1,调用__V函数 { "phone": "${phone}", "xxId": "${_ ...

  3. 吴裕雄 PYTHON 人工智能——基于MASK_RCNN目标检测(5)

    import os import sys import numpy as np import tensorflow as tf import matplotlib import matplotlib. ...

  4. 五、request模块

    描述:requests是python的一个第三方HTTP(Hypertext Transfer Protocol,超文本传输协议)库,它比python自带的网络库urllib更加简单.方便和人性化:使 ...

  5. next_permutation的使用-Hdu1027

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  6. Fluent_Python_Part4面向对象,11-iface-abc,协议(接口),抽象基类

    第四部分第11章,接口:从协议到抽象基类(重点讲抽象基类) 接口就是实现特定角色的方法集合. 严格来说,协议是非正式的接口(只由文档约束),正式接口会施加限制(抽象基类对接口一致性的强制). 在Pyt ...

  7. spring data flow

    spring data flow相当于一个快速发布应用的平台.并可以通过消息队列(kafa,rabbitMQ)把多个应用链接在一起进行链式处理数据.支持的平台是: Cloud Foundry Apac ...

  8. Spring Boot Mybatis 使用教程

    Mybatis 在当下互联网开发环境,十分重要.本章主要讲述 Mybatis 如何使用. 从本系列开始,都需要用到 mysql 数据库 和其他一些参考的数据库.请准备相关环节.本章需要以下环境支撑: ...

  9. Spring学习(十)

    需要的jar包 1.Spring核心必须依赖的库:commons-logging-1.1.1.jar2.Spring IoC部分核心库: spring-beans-4.3.9.RELEASE.jar ...

  10. 配置和验证AP功率

    1.针对自主AP(Autonomous AP) 使用'power local'配置命令配置AP或Bridge Radio功率级别.在2.4 GHz,802.11g Radio上,您可以设置正交频分复用 ...