一、本文章包含的内容
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. (六)责任链模式-C++实现

    使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止. 责任链模式是使用多个对象处理用户请求的成熟模式,它的关键 ...

  2. NOIP2010乌龟棋[DP 多维状态]

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...

  3. AC日记——字符串的展开 openjudge 1.7 35

    35:字符串的展开 总时间限制:  1000ms 内存限制:  65536kB 描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h ...

  4. AC日记——判断字符串是否为回文 openjudge 1.7 33

    33:判断字符串是否为回文 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个字符串,输出该字符串是否回文.回文是指顺读和倒读都一样的字符串. 输入 输入为一行字符串(字符串中 ...

  5. XBOX ONE游戏开发之DEBUG配置(三)

    如何DEBUG 首先打开ADK命令提示窗口 输入命令 xbconnect {XBOX主机的IP} * XBOX主机的IP 在XBOX主机的开发者设置中可以看到,会有一个主机IP和一个工具IP 然后打开 ...

  6. java 27 - 7 反射之 通过反射越过泛型检查

    之前学过的集合里面都有泛型,规定了泛型的类型以后,就不能往这个集合添加除了这个类型之外的类型数据了. 那么,有什么方法可以越过这个泛型,添加特定类型以外的类型数据么? 例子:  往ArrayList& ...

  7. Noip2000 T3 单词接龙

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...

  8. grunt-contrib-uglify压缩插件的常用配置属性

    mangle:false(不混淆变量名和方法名,保留原有的名字),如果不配置,默认混淆,就是将所有方法名和变量都用a,b,c等字母替换 preserveComments : 'all'(不删除注释,还 ...

  9. nodejs里的module.exports和exports的关系

    关于node里面的module.exports和exports的异同,网上已经有很多的资料,很多的文章,很多的博客,看了很多,好像懂了,又好像不懂,过几天又不懂了...大致总结是这样的: //下面这种 ...

  10. Javascript中document.execCommand()的用法

    document.execCommand()方法处理Html数据时常用语法格式如下:document.execCommand(sCommand[,交互方式, 动态参数]) 其中:sCommand为指令 ...