`
lucoo
  • 浏览: 1009 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

activemq初步实践-消费者

 
阅读更多

继上一篇生产者的配置(http://lucoo.iteye.com/blog/2331404)后,

接下来是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发送消息
可以测试效果
分享到:
评论

相关推荐

    activemq-protobuf-1.1-API文档-中文版.zip

    赠送jar包:activemq-protobuf-1.1.jar; 赠送原API文档:activemq-protobuf-1.1-javadoc.jar; 赠送源代码:activemq-protobuf-1.1-sources.jar; 包含翻译后的API文档:activemq-protobuf-1.1-javadoc-API文档-...

    activemq-cpp-library-3.9.5 编译的windows库文件,支持vs2015、vs2017

    《ActiveMQ-CPP Library 3.9.5在Windows环境下的编译与应用》 ActiveMQ-CPP Library 3.9.5是一款专为C++开发者设计的、用于与Apache ActiveMQ集成的库,它提供了丰富的API接口,使得在C++环境中能够方便地发送和...

    activemq-cpp-library-3.9.5-src.zip

    《ActiveMQ-CPP库3.9.5源代码解析与应用》 ActiveMQ-CPP库是Apache ActiveMQ项目的一部分,它提供了一套C++接口,用于与ActiveMQ消息代理进行通信。这个库允许开发者在C++应用程序中实现高级消息队列协议(AMQP)和...

    apache-activemq-5.9.0-bin

    这个“apache-activemq-5.9.0-bin”压缩包包含了Apache ActiveMQ 5.9.0版本的完整二进制文件,用于在本地或网络环境中安装和运行。 Apache ActiveMQ的核心功能包括: 1. **消息队列**:ActiveMQ支持多种消息模式,...

    apache-activemq-5.15.8-bin.zip

    这个"apache-activemq-5.15.8-bin.zip"文件包含了ActiveMQ的可执行版本,用于在本地计算机上安装和运行ActiveMQ服务。 首先,我们需要了解ActiveMQ的核心概念。它是一个消息代理,扮演着消息生产者与消费者之间的...

    activemq-web-console-5.11.2

    activemq-web-console的默认使用方式是通过在activemq.xml中导入jetty.xml配置一个jetty server来实现的。其实activemq-web-console完全可以和activemq-broker分开来部署。 activemq-web-console包含3个apps, 1.一...

    activemq-all-5.2.0.jar包

    activemq-all-5.2.0.JAR包,欢迎下载。编写java中间件的时候会用到。这是activemq实现的jms中间件。希望能帮助到你。

    apache-activemq-5.8.0-bin.zip

    这个压缩包"apache-activemq-5.8.0-bin.zip"包含了ActiveMQ 5.8.0版本的二进制发行版,供用户在本地计算机上安装和运行。 1. **Apache ActiveMQ简介** - Apache ActiveMQ是业界广泛使用的消息代理,提供可靠的消息...

    activemq-pool-5.8.0-sources.jar

    activemq-pool-5.8.0-sources.jar

    activemq-core-5.7.0-API文档-中英对照版.zip

    赠送jar包:activemq-core-5.7.0.jar; 赠送原API文档:activemq-core-5.7.0-javadoc.jar; 赠送源代码:activemq-core-5.7.0-sources.jar; 包含翻译后的API文档:activemq-core-5.7.0-javadoc-API文档-中文...

    activemq-protobuf-1.1.jar

    activemq-protobuf-1.1.jar;activemq-protobuf-1.1.jar

    apache-activemq-5.14.3-bin.zip

    这个"apache-activemq-5.14.3-bin.zip"压缩包包含了在Windows环境下部署和运行ActiveMQ所需的所有文件。让我们深入探讨一下这个版本的ActiveMQ及其在Java消息服务中的应用。 首先,Java消息服务(JMS)是一种标准...

    apache-activemq-5.13.2-bin.tar.gz

    这种通信方式提高了系统的可扩展性和可靠性,因为消息可以在生产者和消费者之间独立传输,即使它们在不同的时间运行或位于不同的网络上。 部署ActiveMQ在Linux环境下,首先需要确保系统已经安装了Java Development ...

    activemq-all-5.8.0.jar

    activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载 activemq-all-5.8.0.jar 下载

    activemq-all-5.6.0.jar

    activemq-all-5.6.0.jar activemq-all-5.6.0.jar activemq-all-5.6.0.jar activemq-all-5.6.0.jar

    activemq-web-4.0-M3.jar.zip

    在提供的压缩包"activemq-web-4.0-M3.jar.zip"中,有两个主要文件:"activemq-web-4.0-M3.jar"和"license.txt"。"activemq-web-4.0-M3.jar"是核心的Java档案文件,包含了运行ActiveMQ Web UI所需的所有类和资源。这...

    apache-activemq-5.5.1-bin.zip加上入门demo

    解压缩apache-activemq-5.5.1-bin.zip,然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。 包含了apache-activemq-5.5.1-bin.zip以及ActiveMQ一个helloworld的demo启动ActiveMQ以后,登陆:...

    activemq-all-5.2.0-sources.jar

    activemq-all-5.2.0-sources.jar

    activemq-all-5.2.0-jar包

    使用`activemq-all-5.2.0.jar`,开发者可以通过JMS API来创建连接、生产者、消费者和消息。此外,ActiveMQ还提供了XML配置文件,用于设置服务器、网络连接、安全策略等。 **部署和运行** 在Java环境中,将`activemq...

    activemq-broker-5.9.1.jar

    activemq-broker-5.9.1.jar,activemq-broker-5.9.1.jar,activemq-broker-5.9.1.jar

Global site tag (gtag.js) - Google Analytics