【ActiveMQ入门-11】ActiveMQ学习-compositeDestination
概要:
前一章讲解了消费者如何通过通配符来匹配目的地,以实现一个消费者同时接收多个目的地的消息。
- JmsMessageListener.java
- Sender.java
- applicationContext-compositeDestination.xml


package com.ll.compositeDestination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;public class JmsMessageListener implements MessageListener {public void onMessage(Message message) {System.out.println("消息全部内容:" + message.toString());try {System.out.println("消息主题:" + message.getJMSDestination().toString());} catch (JMSException e1) {e1.printStackTrace();}TextMessage tm = (TextMessage) message;try {System.out.println("消息体:" + tm.getText());} catch (JMSException e) {e.printStackTrace();}System.out.println("------------------------------------");}}
package com.ll.compositeDestination;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Queue;import javax.jms.Session;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;public class Sender {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-compositeDestination.xml");JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");Destination destination =(Destination) applicationContext.getBean("destinationProducer");// Queue queue = new ActiveMQQueue("FOO.A,FOO.B,FOO.C");//发送消息template.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage("同时向三个Queue中发送相同的消息");}});System.out.println("同时向三个Queue中发送相同的消息-发送完成...");}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!--创建连接工厂 --><bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616"></property></bean><!-- 通配符 供消费者使用 --><bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0" value="FOO.*"></constructor-arg></bean><!-- composite destination 供生产者使用 --><bean id="destinationProducer" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0" value="FOO.A,FOO.B,FOO.C,FOO.D"></constructor-arg></bean><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory"></property><property name="defaultDestination" ref="destinationProducer"></property><property name="receiveTimeout" value="600"></property></bean><!-- 消息监听接口 --><bean id="jmsMessageListener" class="com.ll.compositeDestination.JmsMessageListener"></bean><!-- 消费者,通过消息侦听器实现 --><bean id="consumer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="destination" /><property name="messageListener" ref="jmsMessageListener" /></bean></beans>

环境、jar包和方式1 相同;
- JmsMessageListener.java
- Sender.java
- applicationContext-compositeDestination.xml
package com.ll.compositeDestination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;public class JmsMessageListener implements MessageListener {public void onMessage(Message message) {System.out.println("消息全部内容:" + message.toString());try {System.out.println("消息主题:" + message.getJMSDestination().toString());} catch (JMSException e1) {e1.printStackTrace();}TextMessage tm = (TextMessage) message;try {System.out.println("消息体:" + tm.getText());} catch (JMSException e) {e.printStackTrace();}System.out.println("------------------------------------");}}
package com.ll.compositeDestination;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Queue;import javax.jms.Session;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;public class Sender {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-compositeDestination.xml");JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");Destination destination = (Destination) applicationContext.getBean("destinationProducer");try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}// 发送消息template.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {return session.createTextMessage("同时向多个Queue、Topic中发送相同的消息");}});}}
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"><!--创建连接工厂 --><bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616"></property></bean><!-- 通配符FOO.* 供消费者使用 --><bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0" value="FOO.*"></constructor-arg></bean><!-- 通配符NOTIFY.FOO.* 供消费者使用 --><bean id="destination2" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg index="0" value="NOTIFY.FOO.*"></constructor-arg></bean><!-- composite destination 供生产者使用 ,多个Queue和多个Topic --><bean id="destinationProducer" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg index="0"value="FOO.1,FOO.2,FOO.3,FOO.4,topic://NOTIFY.FOO.D,topic://NOTIFY.FOO.E,topic://NOTIFY.FOO.F"></constructor-arg></bean><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory"></property><property name="defaultDestination" ref="destinationProducer"></property><property name="receiveTimeout" value="600"></property></bean><!-- 消息监听接口 --><bean id="jmsMessageListener" class="com.ll.compositeDestination.JmsMessageListener"></bean><!-- 消息侦听器容器,监听destination --><bean id="consumer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="destination" /><property name="messageListener" ref="jmsMessageListener" /></bean><!-- 消息侦听器容器,监听destination2 --><bean id="consumer2"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="destination2" /><property name="messageListener" ref="jmsMessageListener" /></bean></beans>


【ActiveMQ入门-11】ActiveMQ学习-compositeDestination的更多相关文章
- ActiveMQ入门之四--ActiveMQ持久化方式
消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息中心重新启动后仍然可以将消息发送出去,如果把这种持久化和 ...
- ActiveMQ 入门和与 Spring 整合
ActiveMQ 入门演示 activemq 依赖 <dependency> <groupId>org.apache.activemq</groupId> < ...
- Dubbo入门到精通学习笔记(八):ActiveMQ的安装与使用(单节点)、Redis的安装与使用(单节点)、FastDFS分布式文件系统的安装与使用(单节点)
文章目录 ActiveMQ的安装与使用(单节点) 安装(单节点) 使用 目录结构 edu-common-parent edu-demo-mqproducer edu-demo-mqconsumer 测 ...
- ActiveMQ学习总结(2)——ActiveMQ入门实例教程
1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击a ...
- Dubbo入门到精通学习笔记(十四):ActiveMQ集群的安装、配置、高可用测试,ActiveMQ高可用+负载均衡集群的安装、配置、高可用测试
文章目录 ActiveMQ 高可用集群安装.配置.高可用测试( ZooKeeper + LevelDB) ActiveMQ高可用+负载均衡集群的安装.配置.高可用测试 准备 正式开始 ActiveMQ ...
- 【ActiveMQ入门-5】ActiveMQ学习-消息持久性
ActiveMQ中的消息持久性 ActiveMQ很好的支持了消息的持久性(Persistence).消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是 ...
- Java消息中间件入门笔记 - ActiveMQ篇
入门 消息中间件带来的好处: 1)解耦:系统解耦 2)异步:异步执行 3)横向扩展 4)安全可靠 5)顺序保证 栗子: 通过服务调用让其它系统感知事件发生 系统之间高耦合 程序执行效率低 通过消息中间 ...
- 消息中间件-activemq入门(二)
上一节我们了解了JMS规范并且知道了JMS规范的良好实现者-activemq.今天我们就去了解一下activemq的使用.另外我们应该抱着目的去学习,别忘了我们为什么要使用消息中间件:解耦系统之间的联 ...
- ActiveMQ入门实例
1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击a ...
随机推荐
- Hexo博客搭建教程
1.使用淘宝npm源 $ npm install -g cnpm --registry=https://registry.npm.taobao.org 2.安装hexo cnpm install -g ...
- Time-python
1 datetime datetime是Python处理日期和时间的标准库 1.1 datetime.datetime datetime.datetime.now() ...
- 在junit中添加fail--有test失败即build Failed
项目使用jenkins做持续集成,ant来构建,发现在跑junit单元测试的时候,如果有test case失败了,ci的状态是黄色的unstable,而不是红色的failed,看起来很不爽.个人觉得b ...
- Java数字签名算法--RSA
签名具有的特性: 安全性 抗否认性 数字签名:带有密钥(公钥.私钥)的消息摘要算法(使用私钥进行签名,使用公钥进行验证) 数字签名算法:RSA.DSA.ECDSA 数字签名特性: 验证数据完整性 认证 ...
- 优化cocos2d/x程序的内存使用和程序大小
本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区] 原文链接: http://www.himigame.com/iphone-cocos2d/1043.html ☞ ...
- union-find算法
1.背景 <算法>一书中提到了关于算法的一些基本思想 优秀的算法因为能够解决实际的问题而变得更为重要: 高效算法的代码可以很简单: 理解某个实现的性能特点是一项有趣而令人满足的挑战: 在 ...
- C语言基础:指针初级(补充) 分类: iOS学习 c语言基础 2015-06-10 21:54 19人阅读 评论(0) 收藏
结构体指针:指向结构体指针的变量的指针. 结构体指针指向结构体第一个成员变量的首地址 ->: 指向操作符 定义的指针变量必须指向结构体的首地址,才可以使用 -> 访问结构体成员变量 ...
- arpg网页游戏特效播放(一)
网页游戏中的特效,主要包括:场景特效,攻击特效和UI特效三种.场景特效是在地图层上播放的特效,攻击特效主要是技能触发的一些特效,UI特效是面板上的一些特效,还有一些在人物身上播放的特效,例如脚底光圈特 ...
- error: QXcbConnection: Could not connect to display
/********************************************************************************* * error: QXcbConn ...
- SharePoint 列表多表联合查询
在SharePoint平台二次开发中,我们有时需要涉及多表关联查询展示多列表中的不同字段信息:SharePoint和Sql数据表一样,也支持多表联合查询,但是不像Sql语句那样简单,有一定的局限性,需 ...