Spring整合JMS-基于activeMQ实现(二)
public void onMessage(Message
message) {//这里我们知道生产者发送的就是一个纯文本消息。所以这里能够直接进行强制转换,或者直接把onMessage方法的參数改成Message的子类TextMessageTextMessage textMessage = (TextMessage)message;System. out.println( "接收到一个纯文本消息" );try {System. out.println( "消息内容是:" +
textMessage.getText());} catch (JMSException
e) {e.printStackTrace();}}}
xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xmlns:context= "http://www.springframework.org/schema/context" xmlns:jms= "http://www.springframework.org/schema/jms"xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">
<bean id ="connectionFactory" class= "org.springframework.jms.connection.CachingConnectionFactory" ><property name ="targetConnectionFactory"><bean class= "org.apache.activemq.ActiveMQConnectionFactory" ><property name ="brokerURL"><value >tcp://localhost:61616 </value ></property ></bean ></property ><property name ="sessionCacheSize" value= "1" /></bean >
<!-- Spring jmsTemplate queue --><bean id ="jmsTemplate" class= "org.springframework.jms.core.JmsTemplate" ><property name ="connectionFactory" ref= "connectionFactory"></property ><property name ="defaultDestinationName" value= "subject"></property ><property name ="deliveryPersistent" value= "true"></property ><property name ="pubSubDomain" value="false"></ property> <!--
false p2p,true topic --><property name ="sessionAcknowledgeMode" value= "1"></property ><property name ="explicitQosEnabled" value= "true"></property ><property name ="timeToLive" value="604800000"></ property></bean ><!-- 配置Queue,当中value为Queue名称->start --><bean id = "testQueue" class = "org.apache.activemq.command.ActiveMQQueue" ><constructor-arg index = "0" value ="${pur.test.add}" /></bean ><bean id = "sessionAwareQueue" class = "org.apache.activemq.command.ActiveMQQueue" ><constructor-arg index = "0" value= "queue.liupeng.sessionaware" /></bean ><!-- 配置Queue,当中value为Queue名称->end --><!-- 注入AMQ的实现类属性(JmsTemplate和Destination) --><bean id = "amqQueueSender" class = "com.tuniu.scc.purchase.plan.manage.core.amq.AMQQueueSender" ><property name = "jmsTemplate" ref="jmsTemplate" ></property ><property name = "testQueue" ref="testQueue" ></property ><property name = "sessionAwareQueue" ref= "sessionAwareQueue"></property ></bean ><!-- 消息发送必用的发送类 --><bean id = "multiThreadAMQSender" class ="com.tuniu.scc.purchase.plan.manage.core.amq.MultiThreadAMQSender"init-method= "init"><property name = "jmsTemplate" ref="jmsTemplate" ></property ><property name = "multiThreadAMQExecutor" ref= "multiThreadAMQExecutor" ></property ></bean ><!-- 消息监听器->start --><bean id = "consumerMessageListener" class= "com.tuniu.scc.purchase.plan.manage.core.amq.ConsumerMessageListener" /><!-- 消息监听容器 --><bean id = "jmsContainer" class= "org.springframework.jms.listener.DefaultMessageListenerContainer" ><property name = "connectionFactory" ref= "connectionFactory" /><property name = "destination" ref= "testQueue" /> <!--
消费者队列名称,改动 --><property name = "messageListener" ref= "consumerMessageListener" /></bean ><bean id = "consumerSessionAwareMessageListener" class ="com.tuniu.scc.purchase.plan.manage.core.amq.ConsumerSessionAwareMessageListener" ><property name ="testQueue" ref="testQueue"/> <!--
接收消息后返回给testQueue队列 --></bean >< bean id= "sessionAwareListenerContainer" class= "org.springframework.jms.listener.DefaultMessageListenerContainer" ><property name ="connectionFactory" ref= "connectionFactory" /><property name ="destination" ref="sessionAwareQueue" /><property name ="messageListener" ref= "consumerSessionAwareMessageListener" /></bean ><!-- 消息监听器->end -->
</beans>
private AMQQueueSender amqQueueSender;
private static final Logger LOG =
LoggerFactory.getLogger(AMQController. class);
@UvConfig(method
= "testQueue", description = "測试AMQ")@RequestMapping(value
= "/testQueue", method = RequestMethod. POST)@TSPServiceInfo(name
= "PUR.NM.AMQController.testQueue" ,
description = "測试AMQ")public void testQueue(HttpServletRequest
request, HttpServletResponse response) {try {long beginTime
= System. currentTimeMillis();LOG.info( "发送開始");//amqQueueSender.sendMessage("test", StaticProperty.TEST_QUEUE);amqQueueSender.sendMessage( "test",
StaticProperty.TEST_SESSIONAWARE_QUEUE );LOG.info( "发送结束,耗时:" +(System.currentTimeMillis()-beginTime)+ "ms");} catch (InterruptedException
e) {LOG.error( "測试失败",
e);}}
private Destination testQueue; //返回消息目的队列@Overridepublic void onMessage(TextMessage
message, Session session) throws JMSException {System. out.println( "收到一条消息" );System. out.println( "消息内容是:" +message.getText());MessageProducer producer = session.createProducer( testQueue);Message txtMessage = session.createTextMessage("consumerSessionAwareMessageListener..." );producer.send(txtMessage);}public Destination
getTestQueue() {return testQueue;}public void setTestQueue(Destination
sessionAwareQueue) {this.testQueue =
sessionAwareQueue;}}
收到一条消息消息内容是:test接收到一个纯文本消息消息内容是:consumerSessionAwareMessageListener...
public void handleMessage(String
message) {System. out.println( "ConsumerListener通过handleMessage接收到一个纯文本消息。消息内容是:" +
message);}public void receiveMessage(String
message) {System.out.println("ConsumerListener通过receiveMessage接收到一个纯文本消息。消息内容是:" +
message);}}
- 方法一:
- 、public void sendMessage(Destination destination, final String message) {
- System.out.println("---------------生产者发送消息-----------------");
- System.out.println("---------------生产者发了一个消息:" + message);
- jmsTemplate.send(destination, new MessageCreator() {
- public Message createMessage(Session session) throws JMSException {
- TextMessage textMessage = session.createTextMessage(message);
- textMessage.setJMSReplyTo(responseDestination); //(省略编写其相应的监听器代码)
- return textMessage;
- }
- });
- }

Spring整合JMS-基于activeMQ实现(二)的更多相关文章
- spring整合JMS - 基于ActiveMQ实现
一. 开篇语 继上一篇apache ActiveMQ之初体验后, 由于近期一直在复习spring的东西, 所以本文就使用spring整合下JMS. 二. 环境准备 1. ActiveMQ5.2.0 ( ...
- Spring整合JMS(一)——基于ActiveMQ实现
1.1 JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...
- Spring整合JMS(一)——基于ActiveMQ实现 (转)
*注:别人那复制来的 1.1 JMS简介 JMS的全称是Java Message Service,即Java消 息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者 ...
- 消息中间件ActiveMQ及Spring整合JMS
一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...
- Spring整合JMS(二)——三种消息监听器
原文地址:http://haohaoxuexi.iteye.com/blog/1893676 1.3 消息监听器MessageListener 在Spring整合JMS的应用中我们在定义消息监 ...
- Spring整合JMS(二)——三种消息监听器(转)
*注:别人那复制来的 1.3 消息监听器MessageListener 在Spring整合JMS的应用中我们在定义消息监听器的时候一共可以定义三种类型的消息监听器,分别是MessageList ...
- ActiveMQ (三) Spring整合JMS入门
Spring整合JMS入门 前提:安装好了ActiveMQ ActiveMQ安装 Demo结构: 生产者项目springjms_producer: pom.xml <?xml versio ...
- Spring整合JMS(四)——事务管理
原文链接:http://haohaoxuexi.iteye.com/blog/1983532 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFact ...
- Spring整合JMS——事务管理
Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.JmsTransactio ...
随机推荐
- 在VC下显示JPEG、GIF格式图像的一种简便方法
在VC下显示JPEG.GIF格式图像的一种简便方法 一. 引言 JPEG图像压缩标准随然是一种有损图像压缩标准,但由于人眼视觉的不敏感,经压缩后的画质基本没有发生变化,很快便以较高的压缩率得到了广泛 ...
- 在JAVA中开发应用之html5离线应用
1.环境搭建(Tomcat为例): 在Tomcat中的conf配置文件中web.xml中添加离线配置: <!--HTML5--> <mime-mapping> <ext ...
- Codeforces Round #249 (Div. 2) A B
C好像就是个模拟.D 是个编码复杂度大的,可是好像也就是枚举三角形,我这会儿准备区域赛,尽量找点思维难度大的,所以昨晚A B 还是去做区域赛题吧..... B 也有点意思 贪心 题意:交换相邻两个位的 ...
- 辛星与您解读PHP页面跳转的几种实现方式
因为页面跳转的使用是很频繁的,因此这里给出几种方式,事实上我想我并没有归纳全,毕竟函数那么多,要一下想起来还是特别麻烦的,于是,想到哪里就记到哪里把,等着以后再整理汇总. 第一种方式就是使用heade ...
- 最短路知识点总结(Dijkstra,Floyd,SPFA,Bellman-Ford)
Dijkstra算法: 解决的问题: 带权重的有向图上单源最短路径问题.且权重都为非负值.如果采用的实现方法合适,Dijkstra运行时间要低于Bellman-Ford算法. 思路: 如果存在一条从i ...
- bunoj 34990(hash)
传送门:Justice String 题意:有两个串A,B,问是否存在A的一个子串S,S和B的长度相等,最多有2个字符不同.如果有多个,输出其实下标最小S的下标,没有输出-1. 分析:从A每个位置开始 ...
- MSA2312 enclosure 闪断后
故障描述:由于电源原因,导致整个扩展柜闪断,硬盘全部为leftover状态. 存储划分配置:之前满配的一套MSA2312,划分为4个vd,后面两个vd无影响,前面2个VD都是一半在1号柜子,一半在2号 ...
- C#向并口设备发送指令以获取并口设备的状态
using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Text; usi ...
- 15个nosql
1.MongoDB 介绍 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.主要解决的是海量数据的访问效率问题,为WEB应用提供可扩展的高性能数据存 储解决方案.当数据量达到50GB以 ...
- Android开发者必须深入学习的10个应用开源项目
Android 开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多多相当优秀的应用.其中也有许许多多的开发者提供了应用开源项 目,贡献出他们的智慧和创造力.学习开源代码是掌握技术的 ...