spring整合JMS - 基于ActiveMQ实现
来源:互联网 发布:植丽素怎么样知乎 编辑:程序博客网 时间:2024/04/29 17:11
一. 开篇语
继上一篇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
注意: 测试前请先启动ActiveMQ服务器
三. 代码测试(P2P)
1. MsgSender: 消息生产者
/** * message sender */public class MsgSender {public static void main(String[] args) throws Exception {// load xml and create bean factoryApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");// get JmsTemplate object from spring containerJmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");// get Destination object from spring containerDestination destination = (Destination) ctx.getBean("destination");// send msg to activeMQ serverjmsTemplate.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 factoryApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");// get JmsTemplate object from spring containerJmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");// get Destination object from spring containerDestination destination = (Destination) ctx.getBean("destination");while (true) {// receive msg from activeMQ serverTextMessage 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
7 1
- spring整合JMS - 基于ActiveMQ实现
- spring整合JMS - 基于ActiveMQ实现
- spring整合JMS - 基于ActiveMQ实现
- Spring整合JMS(一)-基于ActiveMQ实现
- Spring整合JMS基于ActiveMQ实现
- JMS--Spring整合JMS(一)——基于ActiveMQ实现
- Spring整合JMS——基于ActiveMQ实现
- Spring整合JMS——基于ActiveMQ实现
- Spring整合JMS(一)——基于ActiveMQ实现
- Spring整合JMS(一)——基于ActiveMQ实现
- Spring整合Jms学习(一)_基于ActiveMQ实现
- Spring整合JMS(一)——基于ActiveMQ实现
- Spring整合JMS(一)---基于activeMQ的实现
- Spring整合JMS——基于ActiveMQ实现
- Spring整合JMS——基于ActiveMQ实现
- Spring整合JMS——基于ActiveMQ实现
- Spring整合JMS(一)——基于ActiveMQ实现
- Spring整合JMS(一)——基于ActiveMQ实现
- FHD、4K、8K为何物
- php 学习笔记1
- 应用于BJ8F101(STM8S903)主控的2.4G标签
- JAVA学习——多态
- mysql对表的操作
- spring整合JMS - 基于ActiveMQ实现
- lol英雄列表显示
- 计算机教师资格证报班不报班的区别
- JAVA学习——多态2
- Android基础--shape的使用
- 那些年
- 浏览器到Servlet的HttpServlet的简单实现、底层实现
- 防不胜防 4K电视和4K屏都有假的!到底怎样才是真4K?
- JAVA学习——多态3