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 ...
随机推荐
- php查找字符串是否存在
strstr //搜索字符串在另一字符串中的首次出现(对大小写敏感) //该函数返回字符串的其余部分(从匹配点).如未找到则返回 false stristr //查找字符串在另一字符串中第一次出现的位 ...
- sql语句用'in'执行多条语句时候,执行错误的解决方法
一般报错是出现,无法将nvarchar类型转换为int类型 这是因为 SqlParameter 带参数 是不能用 , 分割的. 第一种解决方法就是 不用 SqlParameter 带参数的s ...
- 设计模式 - 模板方法模式(template method pattern) JFrame 具体解释
模板方法模式(template method pattern) JFrame 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(templ ...
- android删除文件出错
当删除一个文件,再又一次下载这个同名文件,保存到sdcard时出现error,部分手机出现 Caused by: libcore.io.ErrnoException: open failed: EBU ...
- HDU 1856 More is better(并查集+离散化)
题目地址:HDU 1856 水题.因为标号范围太大,而数据数仅仅有10w,所以要先进行离散化.然后就是裸的并查集了. 代码例如以下: #include <iostream> #includ ...
- 玩转web之JQuery(二)---改变表单和input的可编辑状态(封装的js)
var FormDeal = { /** * 功能 :将表单的所有input都设为可编辑的 *@param 要操作表单的id */ formWritable: function (formId) { ...
- [Windows Phone]解锁、注册Windows Phone实体手机为开发机(Windows 8)
原文:[Windows Phone]解锁.注册Windows Phone实体手机为开发机(Windows 8) 前言 ? ? 最近要开发Windows Phone(以下简称WP)的手机游戏,由於使用模 ...
- CSS: 解决Div float后,父Div无法高度自适应的问题
在用CSS+DIV的布局中,常常会发现,当一个DIV float之后,假设他的高度超过了其父DIV的高度时,其父DIV的高度并不会对应的进行调整.要解决问题(也叫做闭合(清除)浮动),我们有四种办法: ...
- POJ2239 Selecting Courses【二部图最大匹配】
主题链接: http://poj.org/problem?id=2239 题目大意: 学校总共同拥有N门课程,而且学校规定每天上12节可,一周上7天. 给你每门课每周上的次数,和哪一天哪一节 课上的. ...
- iOS:编译错误 linker command failed with exit code 1 (use -v to see invocation)
将project不加入.m要求加入 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzI0MzQ2OQ==/font/5a6L5L2T/fontsi ...