- 浏览: 1183236 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (361)
- java综合 (33)
- 项目管理 (10)
- 工作流 (6)
- spring (11)
- hibenate (17)
- struts (0)
- javascript,html,css,ajax,jquery (11)
- IDE (9)
- 生活 (0)
- 工作 (0)
- 杂记 (1)
- 数据库 (96)
- 服务器 (5)
- 可视编辑 (0)
- freemarker (6)
- 操作系统 windows (13)
- web页面 (6)
- jms (15)
- 调优 (4)
- 测试和bug管理 (2)
- 原理 (1)
- 項目-atf (17)
- 安全 (3)
- xml (4)
- 操作系统 liunx (21)
- 网络 (22)
- office (11)
- 设计 (2)
- 软件 (1)
- 数据库 mysql (6)
- 胖客户端-flex (1)
- 正则 (9)
- oracle- liunx (3)
- sql2000 (2)
- 模式 (1)
- 虚拟机 (2)
- jstl (2)
- 版本控制 打包工具 (0)
- AOP (1)
- demo (1)
- 小软件 (2)
- 感恩 (1)
- iphone 4 (1)
- 反欺诈业务经验整理 (0)
最新评论
-
sea0108:
mark
java内存模型 -
XingShiYiShi:
方便把:testPNR();具体实现发出来吗?谢谢
用正则表达式解析 航信的电子客票和pnr报文 -
wh359126613:
如果js和webservice不在同一个服务器上,有跨域问题如 ...
使用javascript调用webservice示例 -
雨飛雁舞:
...
oracle 动态性能(V$)视图 -
ouyang1224:
好东西
oracle 动态性能(V$)视图
本篇主要讲解在未使用其他框架(Spring)整合情况下,独立基于ActiveMQ,使用JMS规范进行消息通信。
一.JMS回顾
因为ActiveMQ是一个JMS Provider的实现,因此在开始实作前,有必要复习下JMS的基础知识
Java Message Service (JMS)是sun提出来的为J2EE提供企业消息处理的一套规范,JMS目前有2套规范还在使用JMS 1.0.2b和1.1. 1.1已经成为主流的JMS Provider事实上的标准了.
*1.1主要在session上面有一些重要改变,比如支持建立同一session上的transaction,让他支持同时发送P2P(Queue)消息和接受
Topic消息。
在JMS中间主要定义了2种消息模式Point-to-Point (点对点),Publich/Subscribe Model (发布/订阅者),
其中在Publich/Subscribe 模式下又有Nondurable subscription和durable subscription (持久化订阅)2种消息处理方式。
下面是JMS规范基本的接口和实现
JMS Common Interfacse PTP-Specific Interface Pub/Sub-specific interfaces
ConnectionFactory QueueConnectionFactory TopicConnectionFactory
Connection QueueConnection TopicConnection
Destination Queue Topic
Session QueueSession TopiSession
MessageProducer QueueSender TopicPublisher
MessageConsumer QueueReceiver/QueueBrwer TopicSubscriber
二.使用Queue
下面以ActiveMQ example的代码为主进行说明
1. 使用ActiveMQ的Connection,ConnectionFactory 建立连接,注意这里没有用到pool
- import org.apache.activemq.ActiveMQConnection
- import org.apache.activemq.ActiveMQConnectionFactory
//建立Connection
- protected Connection createConnection() throws JMSException, Exception {
- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url);
- Connection connection = connectionFactory.createConnection();
- if (durable && clientID!=null) {
- connection.setClientID(clientID);
- }
- connection.start();
- return connection;
- }
//建立Session
- protected Session createSession(Connection connection) throws Exception {
- Session session = connection.createSession(transacted, ackMode);
- return session;
- }
2。发送消息的代码
//建立QueueSession
- protected MessageProducer createProducer(Session session) throws JMSException {
- Destincation destination = session.createQueue("queue.hello");
- MessageProducer producer = session.createProducer(destination);
- producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- if( timeToLive!=0 )
- producer.setTimeToLive(timeToLive);
- return producer;
- }
//使用Producer发送消息到Queue
- producer.send(message);
3。接受消息,在JMS规范里面,你可以使用
- QueueReceiver/QueueBrowser直接接受消息,但是更多的情况下我们采用消息通知方式,即实现MessageListener接口
- public void onMessage(Message message) {
- //process message
- }
- //set MessageListner ,receive message
- Destincation destination = session.createQueue("queue.hello");
- consumer = session.createConsumer(destination);
- consumer.setMessageListener(this);
以上就是使用jms queue发送接受消息的基本方式
三 Topic
1. 建立连接
- protected Connection createConnection() throws JMSException, Exception {
- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, pwd, url);
- Connection connection = connectionFactory.createConnection();
- //如果你要使用DurableSubScription 方式,你必须为connection设置一个ClientID
- if (durable && clientID!=null) {
- connection.setClientID(clientID);
- }
- connection.start();
- return connection;
- }
2. 建立Session
- protected Session createSession(Connection connection) throws Exception {
- Session session = connection.createSession(transacted, ackMode);
- return session;
- }
3.创建Producer 发送消息到Topic
- //create topic on session
- topic = session.createTopic("topic.hello");
- producer = session.createProducer(topic);
- //send message
- producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
- producer.send(message);
4.创建Consumer接受消息(基本上和Queue相同)
- Destincation destination = session.createTopic("topic.hello");
- MessageConsumer consumer = session.createConsumer(destination);
- consumer.setMessageListener(this);
- //如果你使用的是Durable Subscription方式,你必须在建立connection的时候
- //设置ClientID,而且建立comsumer的时候使用createDurableSubscriber方法,为他指定一个consumerName。
- //connection.setClientID(clientId);
- //consumer = session.createDurableSubscriber((Topic) destination, consumerName);
四:连接ActiveMQ的方式
ActiveMQConnectionFactory 提供了多种连接到Broker的方式activemq.apache.org/uri-protocols.html
常见的有
vm://host:port //vm
tcp://host:port //tcp
ssl://host:port //SSL
stomp://host:port //stomp协议可以跨语言,目前有很多种stomp client 库(java,c#,c/c++,ruby,python...);
发表评论
-
jms&mdp
2010-06-20 17:10 0jms&mdp -
spring rmi 小例子
2009-12-02 17:19 20911 下载 2 导入eclipse (java projec ... -
用ajax发送jms消息,并监听指定队列,并从中取出消息的小例子
2009-11-18 13:41 3342用ajax发送jms消息,并监听指定队列,并从中取出消息的小例 ... -
activemq的小例子
2009-11-18 11:50 1598两种JMS的实现方法: Topic==>消息主题 是发布 ... -
消息队列技术的介绍和原理(MQ)
2009-04-05 01:47 16968消息队列技术是分布式应用间交换信息的一种技术。消息队列可驻留在 ... -
利用JMS Topic发布/订阅消息
2009-03-01 16:20 69292003年3月11日那一期(使用JMS Queue) 解释了如 ... -
基于JMS的数据汇集系统的研究与实现
2009-03-01 12:46 1472摘要 在深入研究JMS异步 ... -
JMS概念和基于weblogic下的JMS配置和客户端调用收藏
2009-03-01 12:01 34741. JMS基本概念 JMS(Java Message Ser ... -
JMS 公共接口
2009-03-01 09:22 1433关键字: JMS ConnectionFactory 是 ... -
在Spring中配置JMS
2009-03-01 08:16 2086郁闷了三天,今天终于把JMS弄出来了,就是发送消息,然后消息 ... -
ActiveMQ 实践之路(四) ActiveMQ 4.x +JBoss 4.x MDP实战篇
2009-03-01 07:53 1871关键字: ActiveMQ Activ ... -
ActiveMQ 实践之路(三) ActiveMQ 4.x +JBoss 4.x 整合篇
2009-03-01 07:51 1490关键字: ActiveMQ ... -
ActiveMQ 实践之路(一) 启动你的ActiveMQ
2009-03-01 07:48 1643ActiveMQ 实践之路(一) 启动你的ActiveMQ ... -
activeMQ 的 Queue 的生产和消费
2009-02-28 06:03 2180概念:两种JMS的实现方法: Topic==>消息主题 ... -
面向消息的中间件和ActiveMQ的介绍
2009-02-28 01:26 2578面向消息的中间件和ActiveMQ的介绍 关键字: MOM J ...
相关推荐
Apache ActiveMQ Queue Topic 详解 教程 加入代码解释说明
一个jms activemq Topic 消息实例 关于jms JMS 是接口,相当于jdbc ,要真正使用它需要某些厂商进行实现 ,即jms provider 常见的jms provider 有 ActiveMQ JBoss 社区所研发的 HornetQ (在jboss6 中默认即可以...
Queue与Topic的比较,学习JMS和activemq必须看的资料之一
springboot2整合activemq的demo内含queue消息和topic消息,需要使用者修改application.yml的连接地址信息就可以了,端口号是默认的可以不用修改
本人在学习activemq,然后 测试完成的demo, 包含了queue,topic,持久化到mysql,订阅模式,包好用
ActiveMQ的两种消息模式的例子,queue和topic,注释很详细,告诉你每步做什么,非springboot整合
# Springboot-activeMQ 本项目基于Spring boot这一平台,整合流行的开源消息队列中间件ActiveMQ,实现一个向ActiveMQ添加和读取消息的功能。...3.主题类型topic,创建主题,生产者发送主题消息,以及消费着消费主题消息
ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS ...activemq的queue和topic JMS中定义了两种消息模型: 点对点(point to point, queue) 发布/订阅(publish/subscribe,topic)。
文章目录ActiveMQ简介1、ActiveMQ简介2、ActiveMQ下载SpringBoot整合ActiveMQ1、新建SpringBoot项目2、项目结构3、相关配置信息4、ActiveMQ配置类Queue队列模式1、队列生产者2、队列消费者3、测试效果Topic模式1、...
spring boot activemq集成示例,包含queue和topic消息的发送、接收,连接池的支持。
activemq-web-console的默认使用方式是通过在activemq.xml中导入jetty.xml配置一个jetty server来实现的。其实activemq-web-console完全可以和activemq-broker分开来部署。 activemq-web-console包含3个apps, 1.一...
把它应用到实际的业务需求中的话我们可以在特定的时候利用生产者生成一消息,并进行发送,对应的消费者在接收到对应的消息后去完成对应的业务逻辑。 JMS 支持两种消息传递模型: 点对点(point-to-point,简称 PTP)...
ActiveMQ是java开发的消息中间件服务。可以支持多种协议(AMQP,MQTT,OpenWire,Stomp),默认的是OpenWire。而python与ActiveMQ的通信使用的是Stomp协议。而如果你的服务没有开启则需要配置开启。 首先需要安装python...
主要介绍了详解Springboot整合ActiveMQ(Queue和Topic两种模式),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
#默认情况下activemq提供的是queue模式 true是可以使用topic,false是仅使用queue模式 spring.jms.pub-sub-domain: true # 设置连接的activemq服务器 spring.activemq.broker-url=failover:(tcp://10.0.1.227:61616,...
activeMQ JMS 3种创建方式 公共方式 QUEUE TOPIC
springboot整合activemq案例,queue,topic两种模式 定时运行和controller请求运行两种方式
MQ是消息中间件,是一种在分布式系统中应用程序借以传递消息的媒介,常用的有ActiveMQ,RabbitMQ,kafka。ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现。?特点:?1、支持多种语言...
启动应用即可测试,可帮助快速了解kafka、activemq 两者在 Queue topic producer consumer 使用异同点,demo仅仅是最简化代码,演示通信和使用,无法对两者的集群模式进行测试,如果有对集群模式有兴趣,可自行扩展...
activemq的Java依赖包,课构建Queue/topic 生产、消费