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下的单元測试
android下的单元測试 在AndroidManifest.xml文件里配置下面信息: 在manifest节点下加入: <!-- 指定測试信息和要測试的包 --> <instrum ...
- [Windows Phone] 地图控制项的经纬度
原文:[Windows Phone] 地图控制项的经纬度 前言 本文主要示范如何使用地图经纬度以及显示地标和行人街道,并透过卷轴控制地图缩放比例的功能. ? 实作 step1 建立专案. ? step ...
- jdk和jre是什么?都有什么用?
大家肯定在安装JDK的时候会有选择是否安装单独的jre,一般都会一起安装,我也建议大家这样做.由于这样更能帮助大家弄清楚它们的差别: Jre 是java runtime environme ...
- netperf 而网络性能测量
本文首先介绍网络性能測量的一些基本概念和方法.然后结合 netperf 工具的使用.详细的讨论怎样測试不同情况下的网络性能. 汤凯 (tangk73@hotmail.com), 2004 年 7 月 ...
- Tian Ji -- The Horse Racin
Tian Ji -- The Horse Racin Problem Description Here is a famous story in Chinese history. "That ...
- UVA434 - Matty's Blocks
option=com_onlinejudge&Itemid=8&page=show_problem&category=457&problem=375&mosms ...
- 取缔Chrome装载电脑管家的广告过滤脚本代码
今天Chrome调试脚本.加载在下面的脚本中找到的内容: /* 电脑管家chrome 广告过滤 */ var GJAD_CS = { elemhideElt : null, setElemhideCS ...
- URAL 1553. Caves and Tunnels 树链拆分
一颗树 每次出发点右键值是0 2操作模式1.第一i右键点值添加x 2.乞讨u至v在这条路上右上方值 树为主的连锁分裂称号 #include <cstdio> #include <cs ...
- T-Sql(一)简单语法
原文:T-Sql(一)简单语法 Sql Server是鄙人学习的第一种数据库,对Sql Server有一种特别的情感,下面就说一下Sql Server的简单语法,适用初学者. 1,创建数据库creat ...
- 【翻译】要理解Ext JS 5小工具
原版的:Understanding Widgets in Ext JS 5 在Ext JS 5,引入了新的"widgetcolumn",支持在网格的单元格中放置组件. 同一时候,还 ...