【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 ...
随机推荐
- Redis (一) 概念安装
一.阿里云安装Redis 1.安装Redis yum -y install redis 2.启动Redis service redis start 或者(推荐使用) systemctl start ...
- Oracle11g dump 部分参数解读
一.Oracle dump expdp CONTENT ALL ALL ,将导出对象定义及其所有数据 DATA_ONLY DATA_ONLY,只导出对象数据 METADATA_ONLY ...
- Pycharm(三)常用设置
File - Settings (ctrl+alt+s) python模板 Editor - File and Code Templates - Python Script 可以使用部分变量. # ! ...
- mysql 函数 事务
mysql 中提供了许多内置函数 CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二字节字符集, LENGTH()返回 ...
- 用tornado实现一个简单的websocket样例
想用SPRING MVC,NODE.JS EXPRESS,TORNADO实现同一个功能,开阔一下视野. 先来TORNADO的吧.. 客户端代码都差不多,主要是服务端代码. TORNADO的说法: ht ...
- WebStrom 多项目展示及vuejs插件安装
2. Vuejs 插件安装: ① ②
- sql,用 ISNULL(), NVL(), IFNULL() and COALESCE() 函数替换空值
在数据库操作中,往往要对一些查询出来的空值进行替换,如函数SUM(),这个函数如果没有值会返回NULL,这是我们不希望看到的, 在MySQL中我们可以这样来写: ) ... 在SQLSERVER中我们 ...
- java 百分比显示Double类型数值
DecimalFormat percent = new DecimalFormat("0.00%"); completed_num = (double) involvedTask_ ...
- 按照Right-BICEP要求设计四则运算2程序的单元测试用例
Right——结果是否正确? B——是否所有的边界条件都是正确的? I——能查一下反响关联吗? C——能用其它手段交叉检查一下吗? E——你是否可以强制错误条件发生? P——是否满足性能要求? 测试计 ...
- 第33课 C++中的字符串类
在C语言中学习字符串时,我们使用的是字符数组的概念. C语言中没有真正意义的字符串.为了表达字符串的概念,我们使用了字符数组来模拟字符串. 在应用程序开发中,我们需要大量的处理字符串,如果还用C语言中 ...