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


注意: 测试前请先启动ActiveMQ服务器


三. 代码测试(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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值