【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 ...
随机推荐
- IScroll的诞生和缺点
转自http://lhdst-163-com.iteye.com/blog/1239784 iscroll.js是Matteo Spinelli开发的一个js文件,使用原生js编写,不依赖与任何js框 ...
- VMware Station NAT上网模式配置
- CriticalSection 临界区
// 临界区.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include<windows.h>#include<iost ...
- L1-035 情人节
以上是朋友圈中一奇葩贴:“2月14情人节了,我决定造福大家.第2个赞和第14个赞的,我介绍你俩认识…………咱三吃饭…你俩请…”.现给出此贴下点赞的朋友名单,请你找出那两位要请客的倒霉蛋. 输入格式: ...
- cocos2d-x移植:xcode到eclipse
xcode程序移植到eclipse 必要组件: 1.macos gcc编译器,若没有,在xcode->preference->downloads中下载command line tools( ...
- Linux服务器没有GUI的情况下使用matplotlib绘图
最近看到关于 python3 中用matplotlib 不进行交互画图,而是直接将图保存到硬盘,主要的一个设置就是 matplotlib.use('agg') 注明: 其实不设置 matplotl ...
- MyEclipse 2014 破解图文详细教程
一.安装完成MyEclipse2014(适用于2013等版本)后,不要打开软件,下载破解附件包. 破解附件包下载地址:点我下载 二.解压破解文件压缩包,得到一下文件列表: 三.双击run.bat,即可 ...
- HDU1081 最大字段和 压缩数组(单调队列优化)
最大字段和题型,推荐做题顺序: HDU1003 HDU1024 HDU1081 ZOJ2975 ZOJ2067 #include<cstdio> #include<cstdlib& ...
- BZOJ3052: [wc2013]糖果公园【树上带修莫队】
Description Input Output Sample Input Sample Input Sample Output 84 131 27 84 HINT 思路 非常模板的树上带修莫队 真的 ...
- tarjan强联通分量(模板)
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #inc ...