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 ...
随机推荐
- vps自己搭建VPN(转)
1.购买一个VPS: https://www.pzea.com/North-America-openvz-vps.html 2.下载putty软件,进行vpn安装: http://www.chiark ...
- Swift难点-继承中的构造规则实例具体解释
关于继承中的构造规则是一个难点. 假设有问题,请留言问我. 我的Swift新手教程专栏 http://blog.csdn.net/column/details/swfitexperience.html ...
- Windows7在自由的虚拟机(微软官方虚拟机)
Windows7在自由的虚拟机(微软官方虚拟机) 前言: 不是说windows7自带的虚拟机最好用,但他的正式版.免费.只是希望你能windows7用户.它将能够自由使用: 还是Vmware. 微软为 ...
- WITH AS
表 id pid name获取 下面所有的子节点
- LoadImage()使用
该系统被定义: WINUSERAPIHANDLEWINAPILoadImageA( HINSTANCE, LPCSTR, UINT, int, int, UINT) ...
- Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内存溢出的Bug
由于android获取图片过大是会出现内存溢出的Bug 07-02 05:10:13.792: E/AndroidRuntime(6016): java.lang.OutOfMemoryError 解 ...
- Silverlight的Socket通信
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPwAAAGwCAIAAAACJJ+TAAAgAElEQVR4nO2deXgT5534Rdhskme7+9
- 重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件
原文:重新想象 Windows 8 Store Apps (22) - 文件系统: 访问文件夹和文件, 通过 AQS 搜索本地文件 [源码下载] 重新想象 Windows 8 Store Apps ( ...
- 简单的反射 把datatable 转换成list对象
/// <summary> /// 把datatable 转换成list对象 /// </summary> /// <typeparam name="T&quo ...
- 自己写的sql排序
create function dbo.Fn_Sort ( @str varchar(1024) ) returns nvarchar(100) as begin declare @tb table ...