- 浏览: 1579022 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (289)
- java 语法基础 (51)
- spring (8)
- mvc struct /Ant --build.xml (8)
- SOA (0)
- oracle 9i/10g (23)
- sql server 2000-2005 (3)
- 数据库基础知识 (6)
- 设计模式与软件架构 (10)
- Hibernate 持久化 (9)
- J2SE/J2EE/J2ME/AJAX 技术 (8)
- JSF 技术 (3)
- JAVA 图形化 (0)
- JMS (40)
- Eclipse 3.2 IDE 开发技巧 (13)
- 项目处理方法集合 (2)
- html/jsp/javascript (2)
- Unix/Linux (9)
- j2me/ARM/windriver/嵌入式 (4)
- 电信科学 (8)
- jsp (1)
- c/c++ (1)
- LZW压缩算法(java) (2)
- Android (77)
- 版本管理git/svn (2)
最新评论
-
huihai:
有demo吗?
NamingStrategy实现动态表名映射 -
cangbaotu:
推荐给大家一些有用的爬虫源码:https://github.c ...
网络爬虫(源代码参考) -
tuspark:
除了.classpath文件以外,.project文件也应该了 ...
Eclipse .classpath文件浅谈 -
tuspark:
造成eclipse自动关闭的原因有很多,这里有很多介绍:ecl ...
eclipse 自动关闭 解决方案 -
DEMONU:
网上都是这些,这种文章。。。
ActiveMQ中的消息持久性
如前所述,JMS分两大类:PTP和Pub/Sub
主要的几个对象:ConnectionFactory、Connection、Destination、Session、MessageProducer、MessageConsumer
其相互关系如下图:
1.PTP:
简单回顾7个对象:QueueConnectionFactory、QuequeConnection、Queue、QueueSession、QueueSender、QueueReceiver、QueueBrowser
1) Sender
try {
//具体怎么得到就不写了,各个系统肯定都不一样,但终究都是通过jdni来获得
Context jndiContext = new InitialContext();
QueueConnectionFactory factory = jndiContext.lookup("**Factory");
Queue queue = jndiContext.lookup("**Queue");
QueueConnection connection = factory.createQueueConnection();
boolean transaction = true;
QueueSession session = connection.createQueueSession(transaction , Session.AUTO_ACKNOWLEDGE);
Message objMessage = session.createObjectMessage(); //或session.createTextMessage("...");
objMessage.setObject((Serializable)obj); //obj为要传输的对象
QueueSender sender = session.createSender(queue);
publisher.setTimeToLive(timeout); //long timeout = ...
connection.start();
sender.send(objMessage);
//or
/**
sender = session.createSender(null);
sender.send(queue, message);
*/
//如果不用了,就收拾干净
publisher.close();
session.close();
connection.close();
} catch (JMSException e) {
//TODO
} finally {
publisher = null;
session = null;
connection = null;
}
2) Receiver/Browser
a.主动接收,synchronously
try {
QueueReceiver receiver = session.createReceiver(queue);
receiver.receiveNoWait();
/**
QueueBrowser browser = session.createBrowser(queue); //QueueBrowser只会取消息,但不会取走消息
Enumeration elements = browser.getEnumeration();
while(elements.hasMoreElements) {
Message message = elements.nextElement();
...
}
*/
} catch (JMSException e) {
...
}
b.消息侦听,也是最主要的应用,asynchronously
try {
MessageListener myListener = new MyListener();
QueueReceiver receiver = session.createReceiver(queue);
receiver.setMessageListener(myListener);
connection.start();
} catch (JMSException e) {
...
}
public class MyListener impelments MessageListner {
public void onMessage(Message message) {
if(message instanceof TextMessage) {
...
} else if (message instanceof ObjectMessage) {
...
} else {
...
}
}
}
2.Pub/Sub:
简单回顾6个对象:TopicConnectionFactory、TopicConnection、Topic、TopicSession、TopicPublisher、TopicSubscriber
1) Publisher
try {
//具体怎么得到就不写了,各个系统肯定都不一样,但终究都是通过jdni来获得
Context jndiContext = new InitialContext();
TopicConnectionFactory factory = jndiContext.lookup("**Factory");
Topic topic = jndiContext.lookup("**Topic");
TopicConnection connection = factory.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); //false表示不使用事务
Message objMessage = session.createObjectMessage();
objMessage.setObject((Serializable)obj); //obj为要传输的对象
TopicPublisher publisher = session.createPublisher(topic);
publisher.setTimeToLive(100000);
connection.start();
publisher.publish(message);
//or
/**
publisher = session.createPublisher(null);
publiser.publish(topic, message);
*/
publisher.close();
session.close();
connection.close();
} catch (JMSException e) {
//TODO
}
2) Subscriber
a.主动接收,比如上面发送完后,接收回应消息,相当于同步接收了
try {
TopicSubscriber subscriber = session.createSubScriber(topic);
message = subscriber.receiver(timeout); //long timeout = ...
subscriber.close();
} catch (JMSException e) {
...
}
b.消息侦听,asynchronously
try {
...
TopicSubscriber subscriber = session.createSubScriber(topic);
MessageListener listener = new MyListener();
subscriber.setMessageListener(listener); //关键在于这个listener
connection.start();
} catch (JMSException e) {
...
}
MyListener实现同上
发表评论
-
基于jms使用ActiveMQ实现异步日志功能.消息持久到oracle 10g 数据库
2008-09-18 15:05 9897package askyaya.entity;import j ... -
activemq 重新连接的机制
2008-09-18 14:39 23174最近一个项目要用到ActiveMq,并且需要最大程度的保证消息 ... -
ActiveMQ stop inactivity read check
2008-09-17 16:01 10519You can do the following to fix ... -
WSAD环境下JMS异步通信全攻略 (3)
2008-09-11 14:18 31783.5 消息驱动的Bean 在前文讨论JMS消息接收处理逻 ... -
WSAD环境下JMS异步通信全攻略 (2)
2008-09-11 13:58 3723三、JMS P2P编程 在JMS P2P通信方式中,发送程 ... -
WSAD环境下JMS异步通信全攻略 (1)
2008-09-11 13:57 4631一、JMS基本概念 1.1 P2P通信 1.2 Pub ... -
topicpublisher (jms)
2008-09-10 18:55 2890目的地类型JNDI名字连接工厂类型Topic/Queuejav ... -
ProducerTool /MessageBroker /getConnectionFactoryF
2008-09-10 18:12 2442lib: jms1.1.jar activemq-all-5. ... -
activemq例子代码 发送BytesMessage消息
2008-09-10 13:55 18327import javax.jms.Connection; im ... -
ActiveMQ in Action(7)
2008-09-10 13:44 3913ActiveMQ in Action(7) 关键字 ... -
ActiveMQ in Action(6)
2008-09-10 13:43 3692ActiveMQ in Action(6) 关键字: acti ... -
ActiveMQ in Action(5)
2008-09-10 13:42 3888ActiveMQ in Action(5) 关键字: acti ... -
ActiveMQ in Action(4)
2008-09-10 13:40 3438ActiveMQ in Action(4) 关键字: acti ... -
ActiveMQ in Action(3)
2008-09-10 13:39 3644ActiveMQ in Action(3) 关键字: acti ... -
ActiveMQ in Action(2)
2008-09-10 13:38 4182ActiveMQ in Action(2) 关键字: acti ... -
ActiveMQ in Action(1)
2008-09-10 13:37 5748ActiveMQ in Action(1) 关键字: acti ... -
为ActiveMQ服务器添加简单验证机制
2008-09-09 23:48 4159为ActiveMQ服务器添加简单验证机制 关键字: Java, ... -
Sender/receiver 消息
2008-09-09 20:28 2090Sender:import java.io.BufferedR ... -
activema.xml 配置
2008-09-09 17:44 3773/***作者:andyao,email:andyaoy@gma ... -
JMX 与系统管理
2008-09-08 10:52 1831Java SE 6 新特性: JMX 与系统管理 ...
相关推荐
JMS实现sub/pub,聊天系统 创建消息的消费者和生产者
SpringBoot集成ActiveMQ,ActiveMQ支持队列和主题两种消息发送方式,选择发送方式可以在SpringBoot的配置文件中通过参数spring.jms.pub-sub-domain来控制,值为false表示是队列,值为true表示是主题。
Java消息服务支持两种消息模型:Point-to-Point消息(P2P)和发布订阅消息(Publish Subscribe messaging,简称Pub/Sub)。JMS规范并不要求供应商同时支持这两种消息模型,但开发者应该熟悉这两种消息模型的优势与缺点...
Java消息服务支持两种消息模型:Point-to-Point消息(P2P)和发布订阅消息(Publish Subscribe messaging,简称Pub/Sub)。JMS规范并不要求供应商同时支持这两种消息模型,但开发者应该熟悉这两种消息模型的优势与缺点...
基于Spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了Point-To-Point的异步队列消息和PUB/SUB(发布/订阅)模型,简单实例,不包含任何业务。
本章我将通过spring jms和activemq实现单Web项目服务器间异步访问和多Web项目服务器间异步访问。 一. 简介 1. 什么是Apache ActiveMq Apache ActiveMq是最流行和最强大的开源消息和集成服务器。同时Apache ActiveMq...
消费形式的不同造成JMS有两组平行的API,这就是JMS的PTP(point to point)模型和PUB/SUB(publisher和subscriber、出版和订阅)模型。PTP的消息应用中一个消息只有一个消费者,消费后该消息即不再有效。而PUB/SUB...
它既支持点到点(point-to-point)(PTP)模型和发布/订阅(Pub/Sub)模型。 *. 支持同步与异步消息发送 *. JDBC持久性管理使用数据库表来存储消息 *. 可视化管理界面。 *. Applet支持。 *. 能够与Jakarta ...
activemq集成spring,和原生的active测试active的p2p和pub/sub方式
Java Message Service(JMS)是SUN提出的旨在统一各种MOM(Message-Oriented Middleware )系统接口的规范,它包含点对点(Point to Point,PTP)和发布/订阅(Publish/Subscribe,pub/sub)两种消息模型,提供可靠...
关于JMS的消息服务的代码,包括PTP和Pub/Sub,简单通俗 易懂。
四、JMS Pub/Sub编程 五、二阶段提交的事务 ━━━━━━━━━━━━━━━━━━━━━━━━━━ EJB 2.0和J2EE 1.3规范开始提供对Java消息服务(JMS)的支持。在J2EE 1.3加入JMS之前,J2EE环境中的组件...
文档目录: 一.JBOSS入门 1.下载和安装JBoss 2.JBoss的目录结构 3.启动服务器 4.JMX控制台 5.停止服务器 6.JBoss中的部署 ...3.2 Topic消息的发送与接收(Pub/sub 消息传递模型) 4.实体Bean(Entity Bean)
Messaging Patterns Point-To-Point Queue/TemporaryQueue QueueBrower Pub/Sub Topic/TemporaryTopic DurableSubscriber
spring.jms.pub-sub-domain: true # 设置连接的activemq服务器 spring.activemq.broker-url=failover:(tcp://10.0.1.227:61616,tcp://10.0.1.228:61616,tcp://10.0.1.229:61616,mqtt://10.0.1.227:1883,mqtt://10.0....
支持发布/订阅(Pub/Sub)和点对点(P2P)消息模型 在一个队列中可靠的先进先出(FIFO)和严格的顺序传递 支持拉(pull)和推(push)两种消息模式 单一队列百万消息的堆积能力 支持多种消息协议,如 JMS、MQTT ...
简单的JMS的代码,里面有p2p,pub/sub方式的通信,有异步和同步消费消息,具体的内容大家可以下载看看
JMS是Java的消息服务,JMS的客户端之间可以通过JMS服务进行异步的消息传输。JMS支持两种消息模型:Point-to-Point(P2P)和Publish/Subscribe(Pub/Sub),即点对点和发布订阅模型。
JavaMessageService(JMS)是SUN提出的旨在统一各种MOM(Message-OrientedMiddleware)系统接口的规范,它包含点对点(PointtoPoint,PTP)和发布/订阅(Publish/Subscribe,pub/sub)两种消息模型,提供可靠消息...