JMS之Spring +activeMQ实现消息队列

来源:互联网 发布:公牛网络插座接法视频 编辑:程序博客网 时间:2024/04/20 13:01

JMS

 

  JMS的全称是Java Message Service,即Java消息服务。它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息。把它应用到实际的业务需求中的话我们可以在特定的时候利用生产者生成一消息,并进行发送,对应的消费者在接收到对应的消息后去完成对应的业务逻辑。对于消息的传递有两种类型,一种是点对点的,即一个生产者和一个消费者一一对应;另一种是发布/订阅模式,即一个生产者产生消息并进行发送后,可以由多个消费者进行接收

 

本质:通过JMS消息服务器,使生产者和消费者之间解耦。这种技术类似于JDBC,同样是一种规范,各个厂商之间对JMS规范进行实现,在使用时,导入相应厂商的实现类,同类产品有activeMQ、JbossMQ、kafka等

 

本文通过Spring集成activeMQ实现一个producer程序、和consumer程序,两者通过MQ进行通讯,具体如下:

 

 1.producer工程和consumer工程目录图

 


 
 
 2.maven依赖

 

Java代码  收藏代码
  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>junit</groupId>  
  4.         <artifactId>junit</artifactId>  
  5.         <version>4.10</version>  
  6.         <scope>test</scope>  
  7.     </dependency>  
  8.     <dependency>  
  9.         <groupId>org.springframework</groupId>  
  10.         <artifactId>spring-context</artifactId>  
  11.         <version>${spring-version}</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.         <groupId>org.springframework</groupId>  
  15.         <artifactId>spring-jms</artifactId>  
  16.         <version>${spring-version}</version>  
  17.     </dependency>  
  18.     <dependency>  
  19.         <groupId>org.springframework</groupId>  
  20.         <artifactId>spring-test</artifactId>  
  21.         <version>${spring-version}</version>  
  22.     </dependency>  
  23.     <dependency>  
  24.         <groupId>javax.annotation</groupId>  
  25.         <artifactId>jsr250-api</artifactId>  
  26.         <version>1.0</version>  
  27.     </dependency>  
  28.     <dependency>  
  29.         <groupId>org.apache.activemq</groupId>  
  30.         <artifactId>activemq-core</artifactId>  
  31.         <version>5.7.0</version>  
  32.     </dependency>  
  33.       
  34.     <dependency>  
  35.         <groupId>org.apache.activemq</groupId>  
  36.         <artifactId>activemq-pool</artifactId>  
  37.         <version>5.13.0</version>  
  38.     </dependency>  
  39.       
  40. </dependencies>  

 

 3.spring-jms.xml配置文件

 

  这里的配置类似于jdbc的配置,需要注意的是消息监听器有几种实现方式,

Java代码  收藏代码
  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.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.      http://www.springframework.org/schema/aop  
  8.      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  9.      http://www.springframework.org/schema/tx   
  10.      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  11.      http://www.springframework.org/schema/context   
  12.      http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  13.         http://cxf.apache.org/jaxws   
  14.         http://cxf.apache.org/schemas/jaxws.xsd">  
  15.   
  16.     <context:component-scan base-package="com.besttone.jms" />    
  17.   
  18.     <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->  
  19.     <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
  20.         <property name="brokerURL" value="tcp://localhost:61616" />  
  21.     </bean>  
  22.   
  23.     <!-- 链接对象进行池化,提高效率,类似于jdbc中的 c3p0/druid等连接池 -->  
  24.     <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">  
  25.         <property name="connectionFactory" ref="targetConnectionFactory" />  
  26.         <property name="maxConnections" value="10" />  
  27.     </bean>  
  28.   
  29.     <bean id="connectionFactory"  
  30.         class="org.springframework.jms.connection.SingleConnectionFactory">  
  31.         <property name="targetConnectionFactory" ref="pooledConnectionFactory" />  
  32.     </bean>  
  33.   
  34.     <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->  
  35.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  36.         <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
  37.         <property name="connectionFactory" ref="connectionFactory" />  
  38.         <!-- 向jmsTemplate注入消息转换器,会自动帮我们转换为我们需要的对象 -->  
  39.         <property name="messageConverter" ref="objConverter"/>    
  40.     </bean>  
  41.   
  42.     <!--这个是队列目的地,点对点的 -->  
  43. <!--     <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">  
  44.         <constructor-arg>  
  45.             <value>besttone</value>  
  46.         </constructor-arg>  
  47.     </bean> -->  
  48.     <!--这个是主题目的地,一对多的 -->  
  49.     <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">  
  50.         <constructor-arg value="topic" />  
  51.     </bean>  
  52.   
  53.     <!-- 默认的消息回复队列 -->  
  54.     <bean id="defaultResponseTopic" class="org.apache.activemq.command.ActiveMQTopic">  
  55.         <constructor-arg>  
  56.             <value>defaultResponseTopic</value>  
  57.         </constructor-arg>  
  58.     </bean>  
  59.   
  60.       
  61.     <!-- 类型转换器 -->    
  62.     <!-- producer: object-> Message ;  consumer: Message -> object -->  
  63.     <bean id="objConverter" class="com.besttone.jms.converter.ObjectConverter"/>  
  64.       
  65.         <!-- 消息监听适配器 -->  
  66.     <bean id="defaultResponseTopicListenerAdapter"  
  67.         class="org.springframework.jms.listener.adapter.MessageListenerAdapter">  
  68.         <property name="delegate">  
  69.             <bean class="com.besttone.jms.listener.ConsumerListener" />  
  70.         </property>  
  71.         <!-- 在监听到该topic有消息时,便调用ConsumerListener内的receiveMessage -->  
  72.         <property name="defaultListenerMethod" value="receiveMessage" />  
  73.           
  74.     </bean>  
  75.       
  76.     <!-- 消息监听适配器对应的监听容器 -->  
  77.     <bean id="defaultResponseTopicListenerAdapterContainer"  
  78.         class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
  79.         <property name="connectionFactory" ref="connectionFactory" />  
  80.         <property name="destination" ref="defaultResponseTopic" />  
  81.         <property name="messageListener" ref="defaultResponseTopicListenerAdapter" />  
  82.     </bean>  
  83.         
  84. </beans>  

 

 

 4.MessageConverter转换类

 

   这个类在producer端,将Object 转换为javax.jms.Message; 在consumer端,可以将javax.jms.Message 转换为Object

 

Java代码  收藏代码
  1. public class ObjectConverter implements MessageConverter {  
  2.   
  3.     public Object fromMessage(Message message) throws JMSException, MessageConversionException {  
  4.         ObjectMessage objMessage = (ObjectMessage) message;    
  5.         return objMessage.getObject();  
  6.     }  
  7.   
  8.     public Message toMessage(Object obj, Session session) throws JMSException, MessageConversionException {  
  9.         return session.createObjectMessage((Serializable) obj);    
  10.     }  
  11.   
  12. }  

 

5.ProducerResponseListener类

    在produce 端发送消息到Topic之后,如果consumer端消费之后,会返回一个消息,这个类就是处理producer接收回复类

 

Java代码  收藏代码
  1. public class ConsumerListener {  
  2.   
  3.     public void receiveMessage(Object obj) {  
  4.         Msg msg = (Msg) obj;  
  5.         if(msg.getCode()==1){  
  6.             System.out.println("回复接收成功~");  
  7.         }else{  
  8.             System.out.println("回复接收失败!");  
  9.         }  
  10.           
  11.     }  
  12. }  

 

6.生产消息类

 

Java代码  收藏代码
  1. jmsTemplate.convertAndSend(destination, obj) // 这个方法会调用上面的转换类  
Java代码  收藏代码
  1. @Component("producerService")  
  2. public class ProducerServiceImpl implements ProducerService {  
  3.       
  4.     @Autowired  
  5.     private JmsTemplate jmsTemplate;    
  6.     
  7.     public void sendObjMsg(Destination destination, Serializable obj) {    
  8.         jmsTemplate.convertAndSend(destination, obj);// 需要定义自己的MessageConverter 或者是使用默认的          
  9.     }   
  10.   
  11. }  

 

7.测试类:

 

Java代码  收藏代码
  1. public class ProducerConsumerTest {  
  2.   
  3.   
  4.     public static void main(String[] args) {  
  5.         ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring-integration.xml");  
  6.         Destination destination =  (Destination) ac.getBean("topicDestination");  
  7.         ProducerService producerService =  (ProducerService) ac.getBean("producerService");  
  8.         for (int i=0; i<1; i++) {    
  9.             Msg msg = new Msg();  
  10.             msg.setCode(0);  
  11.             msg.setMsg("start");  
  12.             producerService.sendObjMsg(destination,msg);    
  13.         }    
  14.     }  
  15. }  

 

 

consumer工程和producer的类似,下面提供下载

1 0
原创粉丝点击