消息队列(三)

ActiveMQ的安装(忽略),主要说明其集成Spring配置使用(点对点模式)。

一、创建Mavne项目,所需ActiveMQ依赖包:

<dependency>

   <groupId>org.apache.activemq</groupId>

   <artifactId>activemq-client</artifactId>

   <version>5.14.5</version>

</dependency>

<dependency>

   <groupId>org.apache.activemq</groupId>

   <artifactId>activemq-pool</artifactId>

   <version>5.14.5</version>

</dependency>

<dependency>

   <groupId>org.springframework</groupId>

   <artifactId>spring-jms</artifactId>

   <version>4.1.9.RELEASE</version>

</dependency>

忽略Spring的集成

二、配置xml

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:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:context="http://www.springframework.org/schema/context"  
  7.        xmlns:p="http://www.springframework.org/schema/p"    
  8.        xmlns:cache="http://www.springframework.org/schema/cache"  
  9.        xsi:schemaLocation="  
  10.             http://www.springframework.org/schema/beans  
  11.             http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  12.             http://www.springframework.org/schema/tx   
  13.             http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  14.             http://www.springframework.org/schema/aop   
  15.             http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  16.             http://www.springframework.org/schema/context        
  17.             http://www.springframework.org/schema/context/spring-context-4.1.xsd   
  18.             http://www.springframework.org/schema/cache   
  19.             http://www.springframework.org/schema/cache/spring-cache-4.1.xsd">  
  20.       
  21.     <context:annotation-config />  
  22.     <context:component-scan base-package="com.test.mq" />  
  23.           
  24.     <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">  
  25.         <property name="brokerURL" value="tcp://127.0.0.1:61616" />  
  26.     </bean>  
  27.       
  28.     <!-- 配置JMS连接工长 -->  
  29.     <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">  
  30.         <constructor-arg ref="amqConnectionFactory" />  
  31.         <property name="sessionCacheSize" value="100" />  
  32.     </bean>  
  33.       
  34.     <!-- 定义消息队列(Queue) -->  
  35.     <bean id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">  
  36.         <!-- 设置消息队列的名字 -->  
  37.         <constructor-arg>  
  38.             <value>mq.demo</value>  
  39.         </constructor-arg>  
  40.     </bean>  
  41.       
  42.     <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->  
  43.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
  44.         <property name="connectionFactory" ref="connectionFactory" />  
  45.         <property name="defaultDestination" ref="demoQueueDestination" />  
  46.         <property name="receiveTimeout" value="10000" />  
  47.         <!-- true是topic,false是queue,默认是false,此处显示写出false -->  
  48.         <property name="pubSubDomain" value="false" />  
  49.     </bean>  
  50.       
  51.     <bean id="queueMessageListener" class="com.test.mq.ActiveMqListener" />  
  52.     <!-- 显示注入消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器 -->  
  53.     <bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">  
  54.         <property name="connectionFactory" ref="connectionFactory" />  
  55.         <property name="destination" ref="demoQueueDestination" />  
  56.         <property name="messageListener" ref="queueMessageListener" />  
  57.     </bean>  
  58.       
  59.  </beans>  

 三、Java代码

Java代码 
  1. package com.test.mq;  
  2.   
  3. import javax.annotation.Resource;  
  4. import javax.jms.Destination;  
  5. import javax.jms.JMSException;  
  6. import javax.jms.Message;  
  7. import javax.jms.Session;  
  8.   
  9. import org.apache.log4j.Logger;  
  10. import org.springframework.jms.core.JmsTemplate;  
  11. import org.springframework.jms.core.MessageCreator;  
  12. import org.springframework.stereotype.Service;  
  13.   
  14. @Service  
  15. public class ActiveMqSender {  
  16.     private static Logger logger = Logger.getLogger(ActiveMqSender.class.getName());  
  17.   
  18.     @Resource  
  19.     private JmsTemplate jmsTemplate;  
  20.   
  21.     /** 
  22.      * 向指定队列发送消息 
  23.      */  
  24.     public void sendMessage(Destination destination, final String msg) {  
  25.         jmsTemplate.send(destination, new MessageCreator() {  
  26.             public Message createMessage(Session session) throws JMSException {  
  27.                 return session.createTextMessage(msg);  
  28.             }  
  29.         });  
  30.         logger.info("向队列" + destination.toString() + "发送了消息------------" + msg);  
  31.     }  
  32.   
  33.     /** 
  34.      * 向默认队列发送消息 
  35.      */  
  36.     public void sendMessage(final String msg) {  
  37.         String destination = jmsTemplate.getDefaultDestination().toString();  
  38.         jmsTemplate.send(new MessageCreator() {  
  39.             public Message createMessage(Session session) throws JMSException {  
  40.                 return session.createTextMessage(msg);  
  41.             }  
  42.         });  
  43.         logger.info("向队列" + destination + "发送了消息------------" + msg);  
  44.     }  
  45.   
  46. }  
Java代码   收藏代码
  1. package com.test.mq;  
  2.   
  3. import javax.jms.JMSException;  
  4. import javax.jms.Message;  
  5. import javax.jms.MessageListener;  
  6. import javax.jms.TextMessage;  
  7.   
  8. import org.apache.log4j.Logger;  
  9.   
  10. public class ActiveMqListener implements MessageListener {  
  11.     private static Logger logger = Logger.getLogger(ActiveMqListener.class.getName());  
  12.   
  13.     @Override  
  14.     public void onMessage(Message message) {  
  15.         TextMessage tm = (TextMessage) message;  
  16.         try {  
  17.             logger.info("ActiveMqListener监听到了文本消息:\t" + tm.getText());  
  18.         } catch (JMSException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     }  
  22.   
  23. }  

 四、测试

Java代码   收藏代码
  1. @Controller  
  2. @RequestMapping("/common")  
  3. public class CommonController {  
  4.     @Resource  
  5.     private ActiveMqSender activeMqSender;  
  6.       
  7.     @RequestMapping(value = "testMq")  
  8.     @ResponseBody  
  9.     public String testMq(int len) {  
  10.         for(int i=0;i<len;i++){  
  11.             activeMqSender.sendMessage("message content "+i);  
  12.         }  
  13.         return "ok";  
  14.     }  
  15. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值