Spring2.5 JMS整和ActiveMQ 5.5

        异步进程通信是面向服务架构(SOA)一个重要的组成部分,因为企业里很多系统通信,特别是与外部组织间的通信,实质上都是异步的。Java消息服务(JMS)是用于编写使用异步消息传递的JEE应用程序的API。传统的使用JMS API进行消息传递的实现包括多个步骤,例如JNDI查询队列连接工厂和Queue资源,在实际发送和接收消息前创建一个JMS会话。

        Spring框架则简化了使用JEE组件(包括JMS)的任务。它提供的模板机制隐藏了典型的JMS实现的细节,这样开发人员可以集中精力放在处理消息的实际工作中,而不用担心如何去创建,访问或清除JMS资源。


集成环境

       Spring采用2.5.6版本,ActiveMQ使用的是5.5.0,从apache站点可以下载。本文是将Spring集成ActiveMQ来发送和接收JMS消息。

环境所需JAR包

applicationContext-service.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<bean id="producerService" class="test.ProducerServiceImpl">
	   <property name="jmsTemplate" ref="jmsTemplate"/>
	   <property name="destination" ref="destination"/>
	</bean>		
 
	<bean id="consumerService" class="test.ConsumerServiceImpl">
	   <property name="jmsTemplate" ref="jmsTemplate"/>
	   <property name="destination" ref="destination"/>
	</bean>		
 
	<!-- 配置Jms模板  -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
	   <property name="connectionFactory" ref="connectionFactory"/>
	   <property name="defaultDestination" ref="destination"/>
	   <property name="receiveTimeout" value="10000"/>
	</bean>
 
	<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="tcp://localhost:61616" />
	</bean>
 
	<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg index="0" value="test" />
	</bean>
</beans>


接收消息:
package test;

public interface ConsumerService {
	public void receive();
}



package test;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;
 
import org.springframework.jms.core.JmsTemplate;

public class ConsumerServiceImpl implements ConsumerService {
	JmsTemplate jmsTemplate;
	 
	Destination destination;
 
	public void receive() {
		TextMessage message = (TextMessage)jmsTemplate.receive();
		try {
			System.out.println(">>接收到的消息>>"+message.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
 
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
 
	public void setDestination(Destination destination) {
		this.destination = destination;
	}
}

发送消息:

package test;

public interface ProducerService {
	public void send();
}


package test;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class ProducerServiceImpl implements ProducerService {
	JmsTemplate jmsTemplate;
	 
	Destination destination;
 
	public void send() {
		MessageCreator messageCreator = new MessageCreator(){
				public Message createMessage(Session session){
					TextMessage message = null;
					try {
						message = session.createTextMessage("你好 Hello");
					} catch (JMSException e) {
						e.printStackTrace();
					}
					return message;
				}};
 
		jmsTemplate.send(this.destination, messageCreator);
	}
 
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}
 
	public void setDestination(Destination destination) {
		this.destination = destination;
	}

}

测试:

package test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
 
	private static ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-service.xml");
 
	private static void send(){
		ProducerService producerService = (ProducerService)appContext.getBean("producerService");
		producerService.send();		
	}
 
	private static void receive(){
		ConsumerService consumerService = (ConsumerService)appContext.getBean("consumerService");
		consumerService.receive();		
	}
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		send();
		receive();
	}
 
}


点击apache-activemq-5.5.0\bin下activemq.bat启动服务


执行Test类的main方法打印信息如下:

>>接收到的消息>>你好 Hello


完毕!


  转载请标明出处 http://blog.csdn.net/shimiso 

技术交流群:361579846

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值