activemq初步实践-消费者

接下来是lion-consumer消费者的配置以及简单应用:

(1)添加核心依赖:

<!--消息机制-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
    <version>${springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-core</artifactId>
    <version>${activemq.version}</version>
</dependency>

 (2)spring配置文件spring-jms.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd">
    <!--connectionFactory-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
    </bean>
    <bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
        <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
        <property name="sessionCacheSize" value="100"/>
    </bean>

    <!--queue监听器-->
<jms:listener-container destination-type="queue" container-type="default" connection-factory="jmsConnectionFactory"
acknowledge="auto">
        <jms:listenerdestination="queueDestination" ref="queueConsumer1"/>
        <jms:listenerdestination="queueDestination" ref="queueConsumer2"/>
    </jms:listener-container>

    <!--topic监听器-->
<jms:listener-container destination-type="topic" container-type="default" connection-factory="jmsConnectionFactory"
acknowledge="auto">
        <jms:listenerdestination="topicDestination" ref="topicSubscriber1"/>
        <jms:listenerdestination="topicDestination" ref="topicSubscriber2"/>
    </jms:listener-container>
</beans>
spring-jmx.xml里面的destination与生产者发送时的destination名称保持一致
(3)同上一篇一样在web容器启动时加载spring-jms.xml
(4)在这里写了两个queue监听器以及两个topic监听器:
QueueConsumer1.java;QueueConsumer2.java;TopicSubsrciber1.java;TopicSucriber2.java
队列消息发送后只有一个监听器可以监听到消息,而topic发送后所有的订阅者都能接收消息

package com.lion.consumer.mq.listener;

import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;

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

/**
 * Created by lucoo on 2016/10/17.
 */
@Component
public class QueueConsumer1 implements SessionAwareMessageListener {
    @Override
public void onMessage(Message message, Session session) throws JMSException {
        String text = ((TextMessage) message).getText();
        System.out.println("队列1接收到的消息:" + text + "============================================");
        session.close();
    }
}


package com.lion.consumer.mq.listener;

import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;

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

/**
 * Created by lucoo on 2016/10/17.
 */
@Component
public class QueueConsumer2 implements SessionAwareMessageListener {
    @Override
public void onMessage(Message message, Session session) throws JMSException {
        String text = ((TextMessage) message).getText();
        System.out.println("队列2接收到的消息:" + text + "============================================");
        session.close();
    }
}

package com.lion.consumer.mq.listener;

import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;

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

/**
 * Created by lucoo on 2016/10/18.
 */
@Component
public class TopicSubscriber1 implements SessionAwareMessageListener {
    @Override
public void onMessage(Message message, Session session) throws JMSException {
        String text = ((TextMessage) message).getText();
        System.out.println("订阅者1接收到的消息:" + text + "============================================");
        session.close();
    }
}

package com.lion.consumer.mq.listener;

import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;

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

/**
 * Created by lucoo on 2016/10/18.
 */
@Component
public class TopicSubscriber2 implements SessionAwareMessageListener {
    @Override
public void onMessage(Message message, Session session) throws JMSException {
        String text = ((TextMessage) message).getText();
        System.out.println("订阅者2接收到的消息:" + text + "============================================");
        session.close();
    }
}

(5)启动activemq,cd到activemq的bin目录下启动即可
(6)启动lion-war和lion-consumer,访问lion-war的JmsController.java里面的url发送消息
可以测试效果

相关推荐