一、本文章包含的内容
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. HttpURLConnection用法详解

    针对JDK中的URLConnection连接Servlet的问题,网上有虽然有所涉及,但是只是说明了某一个或几个问题,是以FAQ的方式来解决的,而且比较零散,现在对这个类的使用就本人在项目中的使用经验 ...

  2. 分布式系统理论之Quorum机制

    一,Quorum机制介绍 在分布式系统中有个CAP理论,对于P(分区容忍性)而言,是实际存在 从而无法避免的.因为,分布系统中的处理不是在本机,而是网络中的许多机器相互通信,故网络分区.网络通信故障问 ...

  3. js中的冒泡排序以及实现一个数组中得最到最大的数字小例

    这其实是一个很简单的js就可以实现,当然一般情况就是利用for循环,从第一个跟第二个开始比较,根据大小交互位置,思路很简单. 也就是js中的冒泡排序 冒泡排序 时间复杂度为O(n^2),有两个优点: ...

  4. JAVA面向对象之一

    一.面向过程的思想和面向对象的思想 面向对象和面向过程的思想有着本质上的区别, 作为面向对象的思维来说,当你拿到一个问题时,你分析这个问题不再是第一步先做什么,第二步再做什么,这是面向过程的思维,你应 ...

  5. 在嵌入式开发板中运行程序提示-/bin/sh: ./xx: not found的解决办法

    今天拿着我的tiny6410板子,在虚拟机上用 $arm-linux-gcc he.c -o he 编译后放到tiny6410的文件系统中提示 -/bin/sh: ./xx: not found 后来 ...

  6. java 28 - 2 设计模式之 模版设计模式

    模版设计模式 模版设计模式概述 模版方法模式就是定义一个算法的骨架,而将具体的算法延迟到子类中来实现 优点 使用模版方法模式,在定义算法骨架的同时,可以很灵活的实现具体的算法,满足用户灵活多变的需求 ...

  7. Sql-Server应用程序的高级注入

    本文作者:Chris Anley 翻译: luoluo [luoluonet@hotmail.com] [目 录] [概要] [介绍] [通过错误信息获取信息] [更深入的访问] [xp_cmdshe ...

  8. IE兼容forEach/map/every/some/indexOf/filter

    some if (!Array.prototype.some){ Array.prototype.some = function(fun /*, thisArg */) { 'use strict'; ...

  9. jquery两个滚动条样式

    jquery两个滚动条样式 点击下载

  10. C语言--指向多维数组的指针和指针数组

    #include <stdio.h> //void show(char *p[]); ]); int main(){ ][]={","abc","x ...