spring整合JMS - 基于ActiveMQ实现
2015-12-23 11:45
357 查看
一. 开篇语
继上一篇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: 消息生产者
2. MsgReceiver: 消息消费者
3. 配置applicationContext.xml
4. 源代码下载地址: http://download.csdn.net/detail/zdp072/7422385
继上一篇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入门Blog[十二、Spring中Annotation声明事务]
- Spring入门Blog[十一、Spring整合Hibernte]
- 重写与重载(区别与用途)
- Spring入门Blog[十、Spring用xml和properties方式配置数据源]
- AES加密时抛出java.security.InvalidKeyException: Illegal key
- Maven搭建Eclipse项目
- Java理论与实践:正确使用volatile 变量
- Java使用7Zip完成文件的压缩和解压缩
- Spring入门Blog[九、Spring Aop 用xml的方式实现]
- maven工程中Springmvc-Mybatis-Mysql的简单整合
- Java编写代理服务器(Burp拦截Demo)一
- Spring入门Blog[八、Spring Aop织入点语法和相关案例总结]
- Java反射机制
- 关于flex同java通信出现的问题
- Spring入门Blog[七、Spring Aop的理解和简单实现]
- 自己动手整合ssh
- Java下的框架编程--cglib的应用
- Spring入门Blog[六、Spring自动装配注解@Autowired]
- Spring入门Blog[五、Spring@Component和Resouces实现注解配置]
- [javase学习笔记]-7.3 构造函数的重载