一、非持久的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使用示例的更多相关文章

  1. ActiveMQ的P2P示例

    ActiveMQ的P2P示例(点对点通信) (1)下载安装activemq,启动activeMQ. 详细步骤参考博客:http://www.cnblogs.com/DFX339/p/9050878.h ...

  2. ActiveMQ topic 普通订阅和持久订阅

    直观的结果:当生产者向 topic 发送消息, 1. 若不存在持久订阅者和在线的普通订阅者,这个消息不会保存,当普通订阅者上线后,它是收不到消息的. 2. 若存在离线的持久订阅者,broker 会为该 ...

  3. ActiveMQ Topic持久化订阅的几点收获

    非持久化模式下,Topic不会落地任何消息,消息入队即出队, 消费者如果想要保留离线后的消息需要告诉MQ实例,即注册过程, 代码上大概是这样的: connectionFactory = new Act ...

  4. ActiveMQ入门操作示例

    1. Queue 1.1 Producer 生产者:生产消息,发送端. 把jar包添加到工程中. 第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号. 第二步:使用Conn ...

  5. ActiveMQ queue 代码示例

    生产者: package com.111.activemq; import javax.jms.Connection; import javax.jms.ConnectionFactory; impo ...

  6. ActiveMQ第一个示例

    首先先安装ActiveMQ:https://www.cnblogs.com/hejianliang/p/9149590.html 创建Java项目,把 activemq-all-5.15.4.jar ...

  7. ActiveMQ使用示例之Topic

    非持久的Topic消息示例  对于非持久的Topic消息的发送基本跟前面发送队列信息是一样的,只是把创建Destination的地方,由创建队列替换成创建Topic,例如: Destination d ...

  8. ActiveMQ笔记(1):编译、安装、示例代码

    一.编译 虽然ActiveMQ提供了发布版本,但是建议同学们自己下载源代码编译,以后万一有坑,还可以尝试自己改改源码. 1.1 https://github.com/apache/activemq/r ...

  9. ActiveMQ入门示例

    1.ActiveMQ下载地址 http://activemq.apache.org/download.html 2.ActiveMQ安装,下载解压之后如下目录

随机推荐

  1. mysql全面优化

    在进行MySQL的优化之前,必须要了解的就是MySQL的查询过程,很多查询优化工作实际上就是遵循一些原则,让MySQL的优化器能够按照预想的合理方式运行而已. 图-MySQL查询过程 一.优化的哲学 ...

  2. H2O框架简介

    H2O框架简介H2O是开源的,分布式的,基于内存的,可扩展的机器学习和预测分析框架,适合在企业环境中构建大规模机器学习模型. H2O核心代码使用Java编写,数据和模型通过分布式 Key/Value ...

  3. python-pptx add_image_picture

  4. JS 判断用户设备 移动端或桌面端

    |)|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAg ...

  5. django模型多对多调用

    对于如下的模型: from django.db import models # Create your models here. class Student(models.Model): name = ...

  6. Realsense D430 save

    rs-save-to-disk.cpp // License: Apache 2.0. See LICENSE file in root directory. // Copyright(c) 2015 ...

  7. 前后端分离session不一致问题

    前端VUE.js 后端SSM 问题描述: 该项目的登录先由后台生成一验证码返回给前端,并保存在session中,不过当前端登录时,后台会报 NullPointerException,看前端的请求头才发 ...

  8. JSONP实现Ajax跨域请求

    前言 由于浏览器存在同源策略的机制,所谓同源策略就是阻止从一个源(域名,包括同一个根域名下的不同二级域名)加载的文档或者脚本获取/或者设置另一个源加载的文档属性. 但比较特别的是:由于同源策略是浏览器 ...

  9. PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

    1041 Be Unique (20 分)   Being unique is so important to people on Mars that even their lottery is de ...

  10. LeetCode_108. Convert Sorted Array to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree Easy Given an array where elements are sorted in asc ...