JMS-activeMq发布订阅模式(非持久订阅)

Publisher的代码:
- import javax.jms.Connection;
- import javax.jms.ConnectionFactory;
- import javax.jms.DeliveryMode;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.MapMessage;
- import javax.jms.MessageProducer;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnectionFactory;
- public class Publisher {
- // 单例模式
- // 1、连接工厂
- private ConnectionFactory connectionFactory;
- // 2、连接对象
- private Connection connection;
- // 3、Session对象
- private Session session;
- // 4、生产者
- private MessageProducer messageProducer;
- public Publisher() {
- try {
- this.connectionFactory = new ActiveMQConnectionFactory("zhangsan",
- "123", "tcp://localhost:61616");
- this.connection = connectionFactory.createConnection();
- this.connection.start();
- // 不使用事务
- // 设置客户端签收模式
- this.session = this.connection.createSession(false,
- Session.AUTO_ACKNOWLEDGE);
- this.messageProducer = this.session.createProducer(null);
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
- }
- public Session getSession() {
- return this.session;
- }
- public void send1(/* String QueueName, Message message */) {
- try {
- Destination destination = this.session.createTopic("topic1");
- MapMessage msg1 = this.session.createMapMessage();
- msg1.setString("name", "张三");
- msg1.setInt("age", 22);
- MapMessage msg2 = this.session.createMapMessage();
- msg2.setString("name", "李四");
- msg2.setInt("age", 25);
- MapMessage msg3 = this.session.createMapMessage();
- msg3.setString("name", "张三");
- msg3.setInt("age", 30);
- // 发送消息到topic1
- this.messageProducer.send(destination, msg1,
- DeliveryMode.NON_PERSISTENT, 4, 1000 * 60 * 10);
- this.messageProducer.send(destination, msg2,
- DeliveryMode.NON_PERSISTENT, 4, 1000 * 60 * 10);
- this.messageProducer.send(destination, msg3,
- DeliveryMode.NON_PERSISTENT, 4, 1000 * 60 * 10);
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
- }
- public void send2() {
- try {
- Destination destination = this.session.createTopic("topic1");
- TextMessage message = this.session.createTextMessage("我是一个字符串");
- // 发送消息
- this.messageProducer.send(destination, message,
- DeliveryMode.NON_PERSISTENT, 4, 1000 * 60 * 10);
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
- }
- public static void main(String[] args) {
- Publisher producer = new Publisher();
- producer.send1();
- }
- }
Subscribe的代码:
- import javax.jms.Connection;
- import javax.jms.ConnectionFactory;
- import javax.jms.Destination;
- import javax.jms.JMSException;
- import javax.jms.MapMessage;
- import javax.jms.Message;
- import javax.jms.MessageConsumer;
- import javax.jms.MessageListener;
- import javax.jms.Session;
- import javax.jms.TextMessage;
- import org.apache.activemq.ActiveMQConnectionFactory;
- public class Subscriber {
- // 单例模式
- // 1、连接工厂
- private ConnectionFactory connectionFactory;
- // 2、连接对象
- private Connection connection;
- // 3、Session对象
- private Session session;
- // 4、生产者
- private MessageConsumer messageConsumer;
- // 5、目的地址
- private Destination destination;
- public Subscriber() {
- try {
- this.connectionFactory = new ActiveMQConnectionFactory("zhangsan",
- "123", "tcp://localhost:61616");
- this.connection = connectionFactory.createConnection();
- this.connection.start();
- // 不使用事务
- // 设置客户端签收模式
- this.session = this.connection.createSession(false,
- Session.AUTO_ACKNOWLEDGE);
- this.destination = this.session.createTopic("topic1");
- this.messageConsumer = this.session.createConsumer(destination);
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
- }
- public Session getSession() {
- return this.session;
- }
- // 用于监听消息队列的消息
- class MyLister implements MessageListener {
- @Override
- public void onMessage(Message message) {
- try {
- if (message instanceof TextMessage) {
- }
- if (message instanceof MapMessage) {
- MapMessage ret = (MapMessage) message;
- System.out.println(ret.toString());
- System.out.println(ret.getString("name"));
- System.out.println(ret.getInt("age"));
- // 因为设置的是客户端的签收模式,所以要手动的去确认消息的消费
- message.acknowledge();
- }
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
- }
- }
- // 用于异步监听消息
- public void receiver() {
- try {
- this.messageConsumer.setMessageListener(new MyLister());
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
- }
- public static void main(String[] args) {
- Subscriber conmuser = new Subscriber();
- conmuser.receiver();
- }
- }



先启动消费者(先订阅后消费),再启动发布者

JMS-activeMq发布订阅模式(非持久订阅)的更多相关文章
- ActiveMQ queue和topic,持久订阅和非持久订阅
消息的 destination 分为 queue 和 topic,而消费者称为 subscriber(订阅者).queue 中的消息只会发送给一个订阅者,而 topic 的消息,会发送给每一个订阅者. ...
- activemq订阅发布模式(非持久订阅)
生产者JMSProducer: package com.sun.test.aircraft.activemq.topic; import org.apache.activemq.ActiveMQCon ...
- JMS学习(五)--ActiveMQ中的消息的持久化和非持久化 以及 持久订阅者 和 非持久订阅者之间的区别与联系
一,消息的持久化和非持久化 ①DeliveryMode 这是传输模式.ActiveMQ支持两种传输模式:持久传输和非持久传输(persistent and non-persistent deliver ...
- JMS学习(六)--提高非持久订阅者的可靠性 以及 订阅恢复策略
一,非持久订阅者 和 实时消费消息 在这篇文章中区分了Domain为Pub/Sub.Destination为Topic时,消费者有两种:持久订阅者 和 非持久订阅者. 对于持久订阅者而言,只要订阅了某 ...
- JMS ActiveMQ研究文档
1. 背景 当前,CORBA.DCOM.RMI等RPC中间件技术已广泛应用于各个领域.但是面对规模和复杂度都越来越高的分布式系统,这些技术也显示出其局限性:(1)同步通信:客户发出调用后,必须等待服务 ...
- ActiveMQ两种模式PTP和PUB/SUB<转>
1.PTP模型 PTP(Point-to-Point)模型是基于队列(Queue)的,对于PTP消息模型而言,它的消息目的是一个消息队列(Queue),消息生产者每次发送消息总是把消息送入消息队列中, ...
- ActiveMQ 事务、集群、持久订阅者、ActiveMQ监控
JMS介绍 JMS是什么? JMS的全称Java Message Service,既Java消息服务. JMS是SUN提供的旨在统一各种MOM(Message-Oriented Middleware) ...
- ActiveMQ 持久订阅者,执行结果与初衷相违背,验证离线订阅者无效,问题解决
导读 最新在接触ActiveMQ,里面有个持久订阅者模块,功能是怎么样也演示不出来效果.配置参数比较简单(配置没几个参数),消费者第一次运行时,需要指定ClientID(此时Broker已经记录离线订 ...
- js设计模式-发布/订阅模式
一.前言 发布订阅模式,基于一个主题/事件通道,希望接收通知的对象(称为subscriber)通过自定义事件订阅主题,被激活事件的对象(称为publisher)通过发布主题事件的方式被通知. 就和用户 ...
随机推荐
- pythn抓取网页小例子
import urllib.request import re from tkinter import * win = Tk() win.geometry('500x300+400+300') t = ...
- KVM(三)I/O 全虚拟化和准虚拟化
在 QEMU/KVM 中,客户机可以使用的设备大致可分为三类: 1. 模拟设备:完全由 QEMU 纯软件模拟的设备. 2. Virtio 设备:实现 VIRTIO API 的半虚拟化设备. 3. PC ...
- RESTful 接口实现简明指南
REST 简介 REST 是一个术语的缩写,REpresentational State Transfer,中文直译「表征状态转移」,这是个很拗口的词.我的建议是先不要强行理解,直接看怎么做,等对实施 ...
- linux下IPTABLES配置详解 (防火墙命令)
linux下IPTABLES配置详解 -A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 24000 -j ACCEPT ...
- HDU 2035.人见人爱A^B-快速幂
人见人爱A^B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 分享HTTP Status 404(The requested resource is not available)的几种解决方案解决方法
下面是直接copy的,如果有什么疑问or补充,请不吝指教! 原文地址:http://www.myexception.cn/java-web/1480013.html 这个问题搞了我两天的时间,找了各种 ...
- 训练指南 UVALive - 3126(DAG最小路径覆盖)
layout: post title: 训练指南 UVALive - 3126(DAG最小路径覆盖) author: "luowentaoaa" catalog: true mat ...
- 简化的INSERT语句
INSERT语句中也并不需要我们指定表中的所有列,比如在插入数据的时候某些字段没有值,我们可以忽略这些字段.下面我们插入一条没有备注信息的数据: INSERT INTO T_Person(FAge,F ...
- Fiddler 高级用法:Fiddler Script 与 HTTP 断点调试
转载自 https://my.oschina.net/leejun2005/blog/399108 1.Fiddler Script 1.1 Fiddler Script简介 在web前端开发的过程中 ...
- 【分块答案】【最小割】bzoj1532 [POI2005]Kos-Dicing
引用zky的题解:http://blog.csdn.net/iamzky/article/details/39667859 每条S-T路径代表一次比赛的结果.最小割会尽量让一个人赢得最多. 因为二分总 ...