Spring2.5 JMS整和ActiveMQ 5.5 http://blog.csdn.net/shimiso/article/details/6712034

   异步进程通信是面向服务架构(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 :

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xmlns:tx="http://www.springframework.org/schema/tx"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  9.            http://www.springframework.org/schema/context  
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  11.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  12.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13.   
  14.     <bean id="producerService" class="test.ProducerServiceImpl">  
  15.        <property name="jmsTemplate" ref="jmsTemplate"/>  
  16.        <property name="destination" ref="destination"/>  
  17.     </bean>         
  18.    
  19.     <bean id="consumerService" class="test.ConsumerServiceImpl">  
  20.        <property name="jmsTemplate" ref="jmsTemplate"/>  
  21.        <property name="destination" ref="destination"/>  
  22.     </bean>         
  23.    
  24.     <!-- 配置Jms模板  -->  
  25.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  26.        <property name="connectionFactory" ref="connectionFactory"/>  
  27.        <property name="defaultDestination" ref="destination"/>  
  28.        <property name="receiveTimeout" value="10000"/>  
  29.     </bean>  
  30.    
  31.     <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
  32.         <property name="brokerURL" value="tcp://localhost:61616" />  
  33.     </bean>  
  34.    
  35.     <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">  
  36.         <constructor-arg index="0" value="test" />  
  37.     </bean>  
  38. </beans>  
<?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>


接收消息:
  1. package test;  
  2.   
  3. public interface ConsumerService {  
  4.     public void receive();  
  5. }  
  6.   
  7.   
  8.   
  9. package test;  
  10. import javax.jms.Destination;  
  11. import javax.jms.JMSException;  
  12. import javax.jms.TextMessage;  
  13.    
  14. import org.springframework.jms.core.JmsTemplate;  
  15.   
  16. public class ConsumerServiceImpl implements ConsumerService {  
  17.     JmsTemplate jmsTemplate;  
  18.        
  19.     Destination destination;  
  20.    
  21.     public void receive() {  
  22.         TextMessage message = (TextMessage)jmsTemplate.receive();  
  23.         try {  
  24.             System.out.println(">>接收到的消息>>"+message.getText());  
  25.         } catch (JMSException e) {  
  26.             e.printStackTrace();  
  27.         }  
  28.     }  
  29.    
  30.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  31.         this.jmsTemplate = jmsTemplate;  
  32.     }  
  33.    
  34.     public void setDestination(Destination destination) {  
  35.         this.destination = destination;  
  36.     }  
  37. }  
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;
	}
}

发送消息:

  1. package test;  
  2.   
  3. public interface ProducerService {  
  4.     public void send();  
  5. }  
  6.   
  7.   
  8. package test;  
  9.   
  10. import javax.jms.Destination;  
  11. import javax.jms.JMSException;  
  12. import javax.jms.Message;  
  13. import javax.jms.Session;  
  14. import javax.jms.TextMessage;  
  15.   
  16. import org.springframework.jms.core.JmsTemplate;  
  17. import org.springframework.jms.core.MessageCreator;  
  18.   
  19. public class ProducerServiceImpl implements ProducerService {  
  20.     JmsTemplate jmsTemplate;  
  21.        
  22.     Destination destination;  
  23.    
  24.     public void send() {  
  25.         MessageCreator messageCreator = new MessageCreator(){  
  26.                 public Message createMessage(Session session){  
  27.                     TextMessage message = null;  
  28.                     try {  
  29.                         message = session.createTextMessage("你好 Hello");  
  30.                     } catch (JMSException e) {  
  31.                         e.printStackTrace();  
  32.                     }  
  33.                     return message;  
  34.                 }};  
  35.    
  36.         jmsTemplate.send(this.destination, messageCreator);  
  37.     }  
  38.    
  39.     public void setJmsTemplate(JmsTemplate jmsTemplate) {  
  40.         this.jmsTemplate = jmsTemplate;  
  41.     }  
  42.    
  43.     public void setDestination(Destination destination) {  
  44.         this.destination = destination;  
  45.     }  
  46.   
  47. }  
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;
	}

}

测试:

  1. package test;  
  2.    
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.    
  6. public class Test {  
  7.    
  8.     private static ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext-service.xml");  
  9.    
  10.     private static void send(){  
  11.         ProducerService producerService = (ProducerService)appContext.getBean("producerService");  
  12.         producerService.send();       
  13.     }  
  14.    
  15.     private static void receive(){  
  16.         ConsumerService consumerService = (ConsumerService)appContext.getBean("consumerService");  
  17.         consumerService.receive();        
  18.     }  
  19.    
  20.     /** 
  21.      * @param args 
  22.      */  
  23.     public static void main(String[] args) {  
  24.         send();  
  25.         receive();  
  26.     }  
  27.    
  28. }  
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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值