spring整合JMS - 基于ActiveMQ实现
一. 开篇语
继上一篇apache ActiveMQ之初体验后, 由于近期一直在复习spring的东西, 所以本文就使用spring整合下JMS.
二. 环境准备
1. ActiveMQ5.2.0 (activemq-all-5.2.0.jar)
2. spring2.5 (spring.jar)
3. JavaEE5
4. JDK1.6
注意: 測试前请先启动ActiveMQserver
三. 代码測试(P2P)
1. MsgSender: 消息生产者
/**
* message sender
*/
public class MsgSender {
public static void main(String[] args) throws Exception {
// load xml and create bean factory
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); // get JmsTemplate object from spring container
JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate"); // get Destination object from spring container
Destination destination = (Destination) ctx.getBean("destination"); // send msg to activeMQ server
jmsTemplate.send(destination, new MessageCreator() {
TextMessage message = null;
public Message createMessage(Session session) {
try {
String str = "hello activeMQ!";
message = session.createTextMessage(str);
System.out.println("send: " + str);
} catch (Exception e) {
throw new RuntimeException("error happens...", e);
}
return message;
}
});
}
}
2. MsgReceiver: 消息消费者
/**
* message receiver
*/
public class MsgReceiver {
public static void main(String[] args) throws Exception {
// load xml and create bean factory
ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); // get JmsTemplate object from spring container
JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate"); // get Destination object from spring container
Destination destination = (Destination) ctx.getBean("destination"); while (true) {
// receive msg from activeMQ server
TextMessage txtmsg = (TextMessage) jmsTemplate.receive(destination);
if (null != txtmsg){
System.out.println("receive: " + txtmsg.getText());
}else{
break;
}
}
}
}
3. 配置applicationContext.xml
<? xml version="1.0" encoding="UTF-8"? >
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- config JMS connection factory -->
<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean> <!-- config JMS template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> <!-- config message send destination(queue) -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- set the name of message queue -->
<constructor-arg index="0" value="myQueue" />
</bean>
</beans>
4. 源代码下载地址: http://download.csdn.net/detail/zdp072/7422385
spring整合JMS - 基于ActiveMQ实现的更多相关文章
- 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 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...
- ActiveMQ (三) Spring整合JMS入门
Spring整合JMS入门 前提:安装好了ActiveMQ ActiveMQ安装 Demo结构: 生产者项目springjms_producer: pom.xml <?xml versio ...
- Spring整合JMS-基于activeMQ实现(二)
Spring整合JMS-基于activeMQ实现(二) 1.消息监听器 在Spring整合JMS的应用中我们在定义消息监听器的时候一共能够定义三种类型的消息监听器,各自是MessageLis ...
- Spring整合JMS(四)——事务管理
原文链接:http://haohaoxuexi.iteye.com/blog/1983532 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFact ...
- Spring整合JMS(二)——三种消息监听器
原文地址:http://haohaoxuexi.iteye.com/blog/1893676 1.3 消息监听器MessageListener 在Spring整合JMS的应用中我们在定义消息监 ...
- Spring整合JMS——事务管理
Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.JmsTransactio ...
- Spring整合JMS(四)——事务管理(转)
*注:别人那复制来的 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.Jm ...
随机推荐
- Android (1) - Activity
onCreate(Bundle status) --> setContentView(View view) --> findViewById(int id) Intent intentFo ...
- 使用scipy进行聚类
近期做图像的时候,突然有个idea,须要进行聚类,事实上算法非常easy,可是当时非常急.就直接使用了scipy的cluster. 使用起来事实上非常easy,可是中文的文章非常少,所以就简单的介绍一 ...
- ORA-00600 [kollasg:client-side tmp lob]
今天在查看一个库的日志时,发现被ORA-00600 [kollasg:client-side tmp lob] 错误刷屏了. 发生该错误的原因是由于应用那边lob的问题.lob没有被初始化,建议使用E ...
- iOS Foundation 框架基类
iOS Foundation 框架基类 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转 ...
- 基于最简单的FFmpeg采样读取内存读写:存储转
===================================================== 基于最简单的FFmpeg样品系列读写内存列表: 最简单的基于FFmpeg的内存读写的样例:内 ...
- unity3D实际的原始视频游戏开发系列讲座12之U3D的2D为了开发实战的新方法
U3D的2D为了开发实战的新方法 (Unity3d-4.x的打飞机2D游戏开发新的方法应用 ) 大纲介绍:不使用NGUI和TK2d插件, 使用 U3D内置强大的最大的工具. 开发过程设计到例 ...
- [TWRP 2.8.4] for 小米2S/2SC 支持中英文切换
其中这个 twrp 2.8.4 在18号的下午已经编译好了. 经历了2个小时的代码修改,再经过后期的调试,中英文双语版本的twrp出炉了. 下面上几张图: 图片1:为英文界面 图片2: 图片3:中文界 ...
- 小米2S 中文和英文支持TWRP,真实双系统支持
经过我几天的努力小米2S的TWRP 的功能已经完美了. 支持功能 : 中文和英文显示能相互切换 真实双系统功能已经完成95%. 刷入手机方法.由于时间原因我只制作了img文件.没有制作成卡刷包格式. ...
- Android.mk参数解释
-------------------- 下面对Android.mk 中经常出现的变量进行讲解 -------------------- 这些变量,你会经常在Android.mk文件中见到,下面以字表 ...
- swift排序算法和数据结构
var arrayNumber: [Int] = [2, 4, 6, 7, 3, 8, 1] //冒泡排序 func maopao(var array: [Int]) -> [Int] { fo ...