一、本文章包含的内容
1、列举了ActiveMQ中监听器的使用
2、spring+activemq方式
1
2
3
<!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue3,监听器是上面定义的监听器 -->
<!--加载spring配置后,studentInfoHandler的onMessage方法自动运行并接收队列-->

 

二、配置文件(spring-jms.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8"?>
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:amq="http://activemq.apache.org/schema/core"
       xsi:schemaLocation="
                    http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/jms
                    http://www.springframework.org/schema/jms/spring-jms-3.2.xsd">
 
    <!-- 启用spring mvc 注解 -->
    <context:component-scan base-package="org.soa.test.activemq"/>
 
    <!-- 配置JMS连接工厂 -->
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="failover:(tcp://192.168.146.129:61616)" />
        <!--解决接收消息抛出异常:javax.jms.JMSException: Failed to build body from content. Serializable class not available to broke-->
        <property name="trustAllPackages" value="true"/>
        <!-- 是否异步发送 -->
        <property name="useAsyncSend" value="true" />
    </bean>
 
    <!--============================监听模块==============-->
    <bean id="queueDestination3" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 设置消息队列的名字 -->
        <constructor-arg>
            <value>queue3</value>
        </constructor-arg>
    </bean>
 
    <bean id="studentInfoHandler" class="org.soa.test.activemq.listeners.StudentInfoHandler" />
 
    <!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue3,监听器是上面定义的监听器 -->
    <!--加载spring配置后,studentInfoHandler的onMessage方法自动运行并接收队列-->
    <bean id="jmsContainer"
          class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="queueDestination3" />
        <property name="messageListener" ref="studentInfoHandler" />
    </bean>
</beans>

三、监听代码

1、监听代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package org.soa.test.activemq.listeners;
 
import org.soa.test.activemq.StudentInfo;
 
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
 
/**
 * Created by JamesC on 16-9-22.
 */
public class StudentInfoHandler implements MessageListener {
 
    //加载spring配置后,studentInfoHandler的onMessage方法自动运行并接收队列
    @Override
    public void onMessage(Message message) {
        TextMessage tm = (TextMessage) message;
        try {
            System.out.println("ConsumerMessageListener收到了文本消息:\t"+ tm.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}

2、测试代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.soa.test.activemq.listeners;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.soa.test.activemq.queues.ProduceMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import javax.jms.Destination;
 
/**
 * Created by JamesC on 16-9-22.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/spring-jms.xml")
public class ListenerTest {
 
    @Autowired
    @Qualifier("queueDestination3")//配置文件中只配置了接收queueDestination3的消息
    private Destination destination;
 
    @Autowired
    private ProduceMsg produceMsg;
 
    @Test
    public void sendMsg(){
        produceMsg.sendMessage(destination,"----这里测试使用配置实现监听器测试消息接收");
    }
}


ActiveMQ_监听器(四)的更多相关文章

  1. Android的按钮单击事件及监听器四种常见的实现方式

    第一种:匿名内部类作为事件监听器类<ignore_js_op>大部分时候,事件处理器都没有什么利用价值(可利用代码通常都被抽象成了业务逻辑方法),因此大部分事件监听器只是临时使用一次,所以 ...

  2. ListView系列(七)——Adapter内的onItemClick监听器四个arg参数 (转)

    举个例子你会理解的更快:X, Y两个listview,X里有1,2,3,4这4个item,Y里有a,b,c,d这4个item.如果你点了b这个item.如下: public void onItemCl ...

  3. Android常见的按钮监听器实现方式

    为按钮(Button)添加响应事件,需要为其设置监听器(Listener).本文总结了Android中常用的几种Button Listener. 第一种:匿名内部类作为事件监听器类 1 2 3 4 5 ...

  4. 4.1 Spring源码 --- 监听器的原理

    目标: 1. 监听器如何使用 2. 监听器的原理 3. 监听器的类型 4. 多播器的概念和作用 5. 接口类型的监听器是如何注册的? 6. 注解类型的监听器和如何注册的? 7. 如果想在所有的bean ...

  5. 2015年12月10日 spring初级知识讲解(三)Spring消息之activeMQ消息队列

    基础 JMS消息 一.下载ActiveMQ并安装 地址:http://activemq.apache.org/ 最新版本:5.13.0 下载完后解压缩到本地硬盘中,解压目录中activemq-core ...

  6. 在有EditText控件的AlertDialog对话框中自动弹出输入法

    我们先回顾一下创建AlertDialog的一般步骤. 一 inflate AlertDialog的布局文件   例如,其中dlg就是我们的布局文件.    View layout = LayoutIn ...

  7. bootstrapTable

    一个详细的教程 table参数 bootstrap table使用总结 BootstrapTable使用实例 事件event 事件函数的用法: 方法1 $('#table').bootstrapTab ...

  8. FastAdmin 基本知识流程一栏

      fastadmin进阶 安装:出现登陆页无法显示:可能是php的gd2扩展未开启 FastAdmin 在 Nginx 中的配置 用的是AdminLTE后台模板require.js.less.Bow ...

  9. spring整合apache-shiro的简单使用

    这里不对shiro进行介绍,介绍什么的没有意义 初学初用,不求甚解,简单使用 一.导入jar包(引入坐标) <!--shiro和spring整合--> <dependency> ...

随机推荐

  1. 第5章 绘图基础_5.1-5.4 GDI绘图

    5.1 GDI的原理和结构 (1)提供一种特殊机制彻底隔离应用程序与不同输出设备(eg.显示器或打印机),以便支持 与设备无关的图形. 光栅设备(如显示器.激光打印机):图像是由点构成的矩阵 图形输出 ...

  2. 另类Unity热更新大法:代码注入式补丁热更新

    对老项目进行热更新 项目用纯C#开发的? 眼看Unity引擎热火朝天,无数程序猿加入到了Unity开发的大本营. 一些老项目,在当时ulua/slua还不如今天那样的成熟,因此他们选择了全c#开发:也 ...

  3. Sass关于颜色函数的乐趣

    阅读目录 1. 了解RGB和HSL颜色标准 2. RGB函数 3. HSL函数 4. Opacity函数 5. 其他颜色函数 6. 一个简单的应用 在Sass中,定义了很多现成的函数,可供我们使用.在 ...

  4. HOLOLENS的空间管理

    http://blog.csdn.net/sun_t89/article/details/52460272

  5. TestLink学习二:Windows搭建TestLink环境

    环境准备: 搭建php5.4.39+apache2.2+mysq5.5.28l环境 (可参考http://www.cnblogs.com/yangxia-test/p/4414161.html) (注 ...

  6. [No000014]听说不背单词,考英语会是这种下场-我们为什么必须背单词?

    由于英语对于一个程序员来说,重要性你懂得.因此我会开始逐渐在博客上加入英语的一些东西. 听说不背单词,考英语会是这种下场 在中国, 「学英语」大抵遵循着这样一条 罗蒙诺索夫质量守恒定律 因为英语学着学 ...

  7. hdu 1542 & & poj 1151

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. javascript的几个小技巧

    1.在循环中缓存array.length 这个技巧很简单,这个在处理一个很大的数组循环时,对性能影响将是非常大的.基本上,大家都会写一个这样的同步迭代的数组. for(var i=0;i<arr ...

  9. CSS3线性渐变和径向渐变

    background:linear-gradient(to top left, blue,orange);//从右下角往左上角渐变 background:radial-gradient(to top ...

  10. HTML 学习笔记 CSS3 (多列)

    CSS3多列 通过CSS3 我们能够创建多个列来对文本进行布局. 在这篇文章中 你将了解到如下多列属性: column-count column-gap column-rule 浏览器支持: 多列的属 ...