ActiveMQ Topic使用示例
一、非持久的Topic
Topic 发送
public class NoPersistenceSender {
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.174.104:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination topic=session.createTopic("myTopic");
MessageProducer producer=session.createProducer(topic);
for(int i=0 ; i<3 ; i++){
TextMessage message=session.createTextMessage("message"+i);
//message.setStringProperty("queue", "queue"+i);
//message.setJMSType("1");
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
Topic 接收
public class NoPersistenceRecever {
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.174.104:61616");
Connection connection = connectionFactory.createConnection();
connection.start();
Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination topic=session.createTopic("myTopic");
MessageConsumer consumer = session.createConsumer(topic);
Message message=consumer.receive();
while (message !=null){
TextMessage textMessage=(TextMessage) message;
//System.out.println(message.getStringProperty("queue"));
System.out.println(textMessage.getText());
session.commit();
message = consumer.receive(1000L);
}
session.close();
connection.close();
}
}
二、持久化得Topic
Topic 发送
public class PersistenceSender {
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.174.104:61616");
Connection connection = connectionFactory.createConnection();
Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination topic=session.createTopic("myTopic1");
MessageProducer producer=session.createProducer(topic);
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
connection.start();
for(int i=0 ; i<3 ; i++){
TextMessage message=session.createTextMessage("message"+i);
//message.setStringProperty("queue", "queue"+i);
//message.setJMSType("1");
producer.send(message);
}
session.commit();
session.close();
connection.close();
}
}
- 要用持久化订阅,发送消息者要用 DeliveryMode.PERSISTENT 模式发现,在连接之前设定
- 一定要设置完成后,再start 这个 connection
Topic 接收
public class PersistenceRecever {
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory=new ActiveMQConnectionFactory("tcp://192.168.174.104:61616");
Connection connection = connectionFactory.createConnection();
connection.setClientID("cc1");
Session session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Topic topic=session.createTopic("myTopic1");
TopicSubscriber ts = session.createDurableSubscriber(topic, "t1");
connection.start();
Message message=ts.receive();
while (message !=null){
TextMessage textMessage=(TextMessage) message;
//System.out.println(message.getStringProperty("queue"));
System.out.println(textMessage.getText());
session.commit();
message = ts.receive(1000L);
}
session.close();
connection.close();
}
}
- 需要在连接上设置消费者id,用来识别消费者
- 需要创建TopicSubscriber来订阅
- 要设置好了过后再start 这个 connection
- 一定要先运行一次,等于向消息服务中间件注册这个消费者,然后再运行客户端发送信息,这个时候,无论消费者是否在线,都会接收到,不在线的话,下次连接的时候,会把没有收过的消息都接收下来。
ActiveMQ Topic使用示例的更多相关文章
- ActiveMQ的P2P示例
ActiveMQ的P2P示例(点对点通信) (1)下载安装activemq,启动activeMQ. 详细步骤参考博客:http://www.cnblogs.com/DFX339/p/9050878.h ...
- ActiveMQ topic 普通订阅和持久订阅
直观的结果:当生产者向 topic 发送消息, 1. 若不存在持久订阅者和在线的普通订阅者,这个消息不会保存,当普通订阅者上线后,它是收不到消息的. 2. 若存在离线的持久订阅者,broker 会为该 ...
- ActiveMQ Topic持久化订阅的几点收获
非持久化模式下,Topic不会落地任何消息,消息入队即出队, 消费者如果想要保留离线后的消息需要告诉MQ实例,即注册过程, 代码上大概是这样的: connectionFactory = new Act ...
- ActiveMQ入门操作示例
1. Queue 1.1 Producer 生产者:生产消息,发送端. 把jar包添加到工程中. 第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号. 第二步:使用Conn ...
- ActiveMQ queue 代码示例
生产者: package com.111.activemq; import javax.jms.Connection; import javax.jms.ConnectionFactory; impo ...
- ActiveMQ第一个示例
首先先安装ActiveMQ:https://www.cnblogs.com/hejianliang/p/9149590.html 创建Java项目,把 activemq-all-5.15.4.jar ...
- ActiveMQ使用示例之Topic
非持久的Topic消息示例 对于非持久的Topic消息的发送基本跟前面发送队列信息是一样的,只是把创建Destination的地方,由创建队列替换成创建Topic,例如: Destination d ...
- ActiveMQ笔记(1):编译、安装、示例代码
一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...
- ActiveMQ入门示例
1.ActiveMQ下载地址 http://activemq.apache.org/download.html 2.ActiveMQ安装,下载解压之后如下目录
随机推荐
- Bean Shell常用内置变量
JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下: log:写入信息到jmeber.log文件,使用方法:log.info( ...
- VSCode前端文件(html文件)如何以服务器模式打开?
方法1: VSCode前端文件(html文件)如何以服务器模式打开?比如工程下有一个A.html文件,想在VSCode里面直接操作,就想Webstorm一样,以http://localhost/xxx ...
- Flutter移动电商实战 --(42)详细页_UI主页面架构搭建
详细分成六大部分拆分开 body里面用FutureBuilder异步加载. FutureBuilder里面的furure属性这里用一个方法,必须返回的也是future 把我们的方法修改为返回的类型为F ...
- maven中的Exclusions详解
依赖关系:Project-A>Project-B>Project-C,但是Project-A不依赖Project-C,在Project-A中的POM.xml应该进行如下配置: <de ...
- 有依赖的背包---P1064 金明的预算方案
P1064 金明的预算方案 solution 1 暴搜 70pt dfs (当前搜到了第几个物品,产生的总价值,剩下多少钱) 剪枝 1:如果剩下的钱数<0,直接return就好,没必要继续了 剪 ...
- [转][PDA]Json.net
文件下载 参考:https://www.cnblogs.com/freezing/p/4125862.html 参考:https://www.cnblogs.com/yunquan/p/7363110 ...
- consul上删除已经注册的节点
1,在consul上找到要删除的ID, 2,在consul的节点上删除这个ID:固定格式: 命令如果运行成功:没有任何的返回值 [root@beta-commonsrv01 ~] $curl --re ...
- 网络爬虫urllib:request之urlopen
网络爬虫urllib:request之urlopen 网络爬虫简介 定义:按照一定规则,自动抓取万维网信息的程序或脚本. 两大特征: 能按程序员要求下载数据或者内容 能自动在网络上流窜(从一个网页跳转 ...
- Mysql迁移由于字符集导致乱码的数据
有时候会在不注意的情况下创建了字符集为latin1的数据库,导致后续插入的中文显示乱码.这时有两种方法:1.修改数据库与数据表的字符集(只能向上调整,不能向下调整):2.数据迁移.但是两种方法都需要做 ...
- (十)java虚拟机性能监控工具
一. jps(Java Virtual Machine Process Status Tool) jps主要用来输出JVM中运行的进程状态信息.语法格式如下: 命令行参数选项说明如下: 1.1 案例 ...