springMVC+MQ 消息队列整合(二)

1、pom.xml依赖的jar

         


            org.apache.activemq
            activemq-spring
            5.14.5
        
        
            org.springframework
            spring-jms
            ${spring.version}
        
2、applicationContext-ActiveMQ.xml配置文件

xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:amq="http://activemq.apache.org/schema/core"
       xmlns:jms="http://www.springframework.org/schema/jms"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.14.5.xsd"
>
    <amq:connectionFactory id="amqConnectionFactory"
                           brokerURL="${activemq_url}"
                           userName="${activemq_username}"
                           password="${activemq_password}" />

    
    id="connectionFactory"
          class="org.springframework.jms.connection.CachingConnectionFactory">
        ref="amqConnectionFactory" />
        name="sessionCacheSize" value="100" />
    

    
    id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        
        
            ${activemq_topic}
        
    

    id="demoTopicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        
        
            ${activemq_topic}
        
    


    
    id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        name="connectionFactory" ref="connectionFactory" />
        name="defaultDestination" ref="demoTopicDestination" />
        name="receiveTimeout" value="10000" />
        
        name="pubSubDomain" value="true" />
    
    id="topicMessageListen" class="com.ultrapower.secsight.core.filter.TopicMessageListener"  />

    
    id="queueListenerContainer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        name="connectionFactory" ref="connectionFactory" />
        name="destination" ref="demoTopicDestination" />
        name="messageListener" ref="topicMessageListen" />
    

3.配置文件ActiveMQ.properties

##MQ 服务器
activemq_url=tcp://192.168.170.69:61616
##用户名
activemq_username=admin
#密码
activemq_password=admin
#主题
activemq_topic=TOPIC

4、代码监听器

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @author xiefg
 * @create 2017-09-11 16:35
 * @desc
 **/
public class TopicMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        TextMessage tm = (TextMessage) message;
        try {
            System.out.println("TopicMessageListener监听到了文本消息:\t"
                    + tm.getText());
            //do something ...
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
5、注意配置文件中加载以下代码,可以用${}获取ActiveMQ.properties值


   id="propertyConfigurer"
   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   name="locations">
      
         classpath:ActiveMQ.properties
      
   

6、进入服务器管理界面点击j页面上的 send ,看图上标注写

springMVC+MQ 消息队列整合(二)_第1张图片


点击下面的send  发送主题消息,java web配置的监听器,会监听到相应的主题,web启动后看看后台是否有相关输出,输出 一下信息表示成功

TpicMessageListener监听到了文本消息:

你可能感兴趣的:(消息队列)