ActiveMQ与spring集成实例之使用消息监听器

消息监听器容器代码  收藏代码
  1.     在EJB世界里,JMS消息最常用的功能之一是用于实现消息驱动Bean(MDB)。Spring提供了一个方法来创建消息驱动的POJO(MDP),并且不会把用户绑定在某个EJB容器上。  
  2.   
  3.    通常用消息监听器容器从JMS消息队列接收消息并驱动被注射进来的MDP。消息监听器容器负责消息接收的多线程处理并分发到各MDP中。一个消息侦听容器是MDP和消息提供者之间的一个中介,用来处理消息接收的注册,事务管理的参与,资源获取和释放,异常转换等等。这使得应用开发人员可以专注于开发和接收消息(可能的响应)相关的(复杂)业务逻辑,把和JMS基础框架有关的样板化的部分委托给框架处理。   
  4.   
  5. Spring提供了三种 AbstractMessageListenerContainer 的子类,每种各有其特点。  
  6.    
  7. 第一种:SimpleMessageListenerContainer  
  8.   
  9.    这个消息侦听容器是三种中最简单的。它在启动时创建固定数量的JMS session并在容器的整个生命周期中使用它们。这个类不能动态的适应运行时的要求或参与消息接收的事务处理。然而它对JMS提供者的要求也最低。它只需要简单的JMS API。  
  10.   
  11. 第二种:DefaultMessageListenerContainer  
  12.   
  13.    这个消息侦听器使用的最多。和 SimpleMessageListenerContainer 相反,这个子类可以动态适应运行时侯的要求,也可以参与事务管理。每个收到的消息都注册到一个XA事务中(如果使用 JtaTransactionManager 配置过),这样就可以利用XA事务语义的优势了。这个类在对JMS提供者的低要求和提供包括事务参于等的强大功能上取得了很好的平衡。  
  14.   
  15. 第三种:ServerSessionMessageListenerContainer  
  16.   
  17.    这个监听器容器利用JMS ServerSessionPool SPI动态管理JMS Session。 使用者各种消息监听器可以获得运行时动态调优功能,但是这也要求JMS提供者支持ServerSessionPool SPI。如果不需要运行时性能调整,请使用 DefaultMessageListenerContainer 或 SimpleMessageListenerContainer。   

 

自定义消息监听器代码  收藏代码
  1. /*  
  2.  * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA).  
  3.  * All Rights Reserved.  
  4.  */  
  5. package com.acca.activemq.listener;  
  6.   
  7. import javax.jms.JMSException;  
  8. import javax.jms.Message;  
  9. import javax.jms.MessageListener;  
  10. import javax.jms.ObjectMessage;  
  11.   
  12. import com.acca.activemq.entity.User;  
  13.   
  14. /**  
  15.  *   
  16.  * 自定义消息侦听器  
  17.  *   
  18.  * @author zhouhua, 2012-12-26  
  19.  */  
  20. public class MyMessageListener implements MessageListener {  
  21.   
  22.     /**  
  23.      * @param arg0  
  24.      * @see javax.jms.MessageListener#onMessage(javax.jms.Message)  
  25.      */  
  26.     @Override  
  27.     public void onMessage(Message message) {  
  28.        System.out.println(message.toString());  
  29.        System.out.println("MyMessageListener");  
  30.        if(message instanceof ObjectMessage){  
  31.            ObjectMessage objectMessage=(ObjectMessage) message;  
  32.            try {  
  33.             User user=(User) objectMessage.getObject();  
  34.             System.out.println(user.getUserName());  
  35.         } catch (JMSException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.        }  
  39.     }  
  40.   
  41. }  

 

自定义消息转换器代码  收藏代码
  1. /*  
  2.  * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA).  
  3.  * All Rights Reserved.  
  4.  */  
  5. package com.acca.activemq.convert;  
  6.   
  7. import java.io.Serializable;  
  8.   
  9. import javax.jms.JMSException;  
  10. import javax.jms.Message;  
  11. import javax.jms.Session;  
  12.   
  13. import org.apache.activemq.command.ActiveMQObjectMessage;  
  14. import org.springframework.jms.support.converter.MessageConversionException;  
  15. import org.springframework.jms.support.converter.MessageConverter;  
  16.   
  17. import com.acca.activemq.entity.User;  
  18.   
  19. /**  
  20.  *   
  21.  * 消息转换器  
  22.  *   
  23.  * @author zhouhua, 2012-12-25  
  24.  */  
  25. public class MyConvert implements MessageConverter {  
  26.   
  27.     /**  
  28.      * @param arg0  
  29.      * @return  
  30.      * @throws JMSException  
  31.      * @throws MessageConversionException  
  32.      * @see org.springframework.jms.support.converter.MessageConverter#fromMessage(javax.jms.Message)  
  33.      */  
  34.     @Override  
  35.     public Object fromMessage(Message message) throws JMSException, MessageConversionException {  
  36.         User user=null;  
  37.             if(message instanceof ActiveMQObjectMessage){  
  38.                 ActiveMQObjectMessage aMsg = (ActiveMQObjectMessage) message;   
  39.                  user=(User) aMsg.getObject();  
  40.             }  
  41.         return user;  
  42.     }  
  43.   
  44.     /**  
  45.      * @param arg0  
  46.      * @param arg1  
  47.      * @return  
  48.      * @throws JMSException  
  49.      * @throws MessageConversionException  
  50.      * @see org.springframework.jms.support.converter.MessageConverter#toMessage(java.lang.Object, javax.jms.Session)  
  51.      */  
  52.     @Override  
  53.     public Message toMessage(Object object, Session session) throws JMSException,  
  54.         MessageConversionException {  
  55.           
  56.         ActiveMQObjectMessage msg = (ActiveMQObjectMessage) session.createObjectMessage();  
  57.         msg.setObject((Serializable) object);  
  58.         return msg;   
  59.     }  
  60.   
  61. }  

 

消息发送代码  收藏代码
  1. /*  
  2.  * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA).  
  3.  * All Rights Reserved.  
  4.  */  
  5. package com.acca.activemq;  
  6.   
  7. import javax.jms.Destination;  
  8. import javax.jms.JMSException;  
  9. import javax.jms.Message;  
  10. import javax.jms.Session;  
  11.   
  12. import org.springframework.context.ApplicationContext;  
  13. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  14. import org.springframework.jms.core.JmsTemplate;  
  15. import org.springframework.jms.core.MessageCreator;  
  16.   
  17. import com.acca.activemq.entity.User;  
  18.   
  19. /**  
  20.  *   
  21.  *   
  22.  *  
  23.  * @author zhouhua, 2012-12-26  
  24.  */  
  25. public class Sender {  
  26.       
  27.     public static void main(String[] args) {  
  28.           
  29.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");  
  30.   
  31.         JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");  
  32.   
  33.         Destination destination = (Destination) applicationContext.getBean("destination");  
  34.         User user=new User();  
  35.         user.setUserName("张三");  
  36.         template.convertAndSend(user);  
  37.         System.out.println("成功发送了一条JMS消息");  
  38.   
  39.     }  
  40. }  

 

实体类user,必须实现序列化代码  收藏代码
  1. /*  
  2.  * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA).  
  3.  * All Rights Reserved.  
  4.  */  
  5. package com.acca.activemq.entity;  
  6.   
  7. import java.io.Serializable;  
  8.   
  9. /**  
  10.  *   
  11.  *   
  12.  *   
  13.  * @author zhouhua, 2012-12-26  
  14.  */  
  15. public class User implements Serializable{  
  16.   
  17.     private String userName;  
  18.   
  19.     /**  
  20.      * @return the userName  
  21.      */  
  22.     public String getUserName() {  
  23.         return userName;  
  24.     }  
  25.   
  26.     /**  
  27.      * @param userName the userName to set  
  28.      */  
  29.     public void setUserName(String userName) {  
  30.         this.userName = userName;  
  31.     }  
  32.   
  33. }  

 

配置文件代码  收藏代码
  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" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  5.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  6.         http://www.springframework.org/schema/context   
  7.         http://www.springframework.org/schema/context/spring-context-2.5.xsd"  
  8.     default-autowire="byName">  
  9.   
  10.   
  11.     <!-- 配置connectionFactory -->  
  12.     <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"  
  13.         destroy-method="stop">  
  14.         <property name="connectionFactory">  
  15.             <bean class="org.apache.activemq.ActiveMQConnectionFactory">  
  16.                 <property name="brokerURL">  
  17.                     <value>tcp://127.0.0.1:61616</value>  
  18.                 </property>  
  19.             </bean>  
  20.         </property>  
  21.         <property name="maxConnections" value="100"></property>  
  22.     </bean>  
  23.   
  24.     <!-- Spring JMS Template -->  
  25.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  26.         <property name="connectionFactory">  
  27.             <ref local="jmsFactory" />  
  28.         </property>  
  29.         <property name="defaultDestinationName" value="subject" />  
  30.         <!-- 区别它采用的模式为false是p2p,为true是订阅 -->  
  31.         <property name="pubSubDomain" value="true" />  
  32.         <property name="messageConverter" ref="myConvert"></property>  
  33.     </bean>  
  34.   
  35.     <!-- 发送消息的目的地(一个队列) -->  
  36.     <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">  
  37.         <!-- 设置消息队列的名字 -->  
  38.         <constructor-arg index="0" value="subject" />  
  39.     </bean>  
  40.       
  41.     <!-- 消息监听  -->    
  42.     <bean id="listenerContainer"    
  43.         class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
  44.         <!-- 消息监听器输出消息的数量 -->    
  45.         <property name="concurrentConsumers" value="1" />    
  46.         <property name="connectionFactory" ref="jmsFactory" />    
  47.         <property name="destinationName" value="subject" />    
  48.         <property name="messageListener" ref="myMessageListener" />    
  49.         <property name="pubSubNoLocal" value="false"></property>    
  50.     </bean>   
  51.        
  52.     <!-- 消息转换器 -->  
  53.     <bean id="myConvert" class="com.acca.activemq.convert.MyConvert">  
  54.     </bean>  
  55.   
  56.    <!-- 消息侦听器 -->  
  57.    <bean id="myMessageListener" class="com.acca.activemq.listener.MyMessageListener"></bean>  
  58.   
  59.    <!--   
  60.     <bean id="messageReceiver" class="com.acca.activemq.ProxyJMSConsumer">  
  61.         <property name="jmsTemplate" ref="jmsTemplate"></property>  
  62.         <property name="destination" ref="destination"></property>  
  63.     </bean>  
  64.     -->  
  65.   
  66.       
  67.   
  68. </beans> 
posted @ 2017-10-11 11:40  IT笨笨IT  阅读(168)  评论(0)    收藏  举报