一、本文章包含的内容
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. 《2016ThoughtWorks技术雷达峰会----微服务架构》

    微服务架构   王键,ThoughtWorks, 首席咨询师 首先微服务架构的定义,thoughtWorks在2012年3月的技术雷达中这样定义: “微服务架构是一种架构,它提倡将单一应用程序划分为一 ...

  2. Stanford机器学习笔记-4. 神经网络Neural Networks (part one)

    4. Neural Networks (part one) Content: 4. Neural Networks (part one) 4.1 Non-linear Classification. ...

  3. CF 371B Fox Dividing Cheese[数论]

    B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. 异常总结<经典例题>

    public class Test1 { public static void main(String[] args) { try { add(1); System.out.println(" ...

  5. Windows 2008 R2 64位上安装wamp失败的原因

    Exception Exception in module wampmanager.exe at 000F15A0... 因测试PHP程序需要,需要在windows系统上布署WAMP环境测试程序,对性 ...

  6. 类集-collection接口

    类集就是一个动态的对象数组,与一般的对象数组不同,类集的对象类容可以随意扩充. 1,对象数组使用的时候会存在一个长度的限制,那么类集是专门解决这种限制的.使用类集可以向数组增加任意多的数据. 2,对象 ...

  7. Screen 对象

    Screen 对象 Screen 对象 Screen 对象包含有关客户端显示屏幕的信息. 注意: 没有应用于 screen 对象的公开标准,不过所有浏览器都支持该对象. Screen 对象属性 属性 ...

  8. 你所未知的3种 Node.js 代码优化方式

    from:https://cnodejs.org/topic/56cc2fd6c045c3743304bec6 Node.js 程序的运行可能会受 CPU 或输入输出操作的限制而十分缓慢.从 CPU ...

  9. 深入运用js

    1,eval()函数 这个函数是获取参数的字符串,并将其作为js来处理,所以这里就有可能有人用这个来搞破坏(比如注入JS脚本文件等),所以最好的是方法是尽量少用,或者可以用new function() ...

  10. Mysql导出函数、存储过程

    下面是导出存储过程的代码 1 # mysqldump -u 数据库用户名 -p -n -t -d -R 数据库名 > 文件名 其中,-d 表示--no-create-db, -n表示--no-d ...