ActiveMQ_Topic队列(三)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!--activemq Begin--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <!-- <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>${spring.version}</version> </dependency>--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.14.0</version> </dependency> <!--activemq End--> |
2、activemq的配置文件: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
46
47
48
49
|
<!-- 启用spring mvc 注解 --> <context:component-scan base-package="org.soa.test.activemq"/> <!-- 配置JMS连接工厂 --> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <!--解决接收消息抛出异常: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> <!-- Topic模式 Begin --> <!-- 定义消息队列名称 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg> <value>topic1</value> </constructor-arg> </bean> <!-- 配置JMS模板,Spring提供的JMS工具类,它发送、接收消息。(Topic) --> <bean id="jmsTemplateTopic" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="topicDestination" /> <!-- 订阅发布模式 --> <property name="pubSubDomain" value="true"/> </bean> <!-- 消息主题监听者 和 主题监听容器 可以配置多个,即多个订阅者 --> <!-- 消息主题监听者(Topic) --> <bean id="topicMessageListener1" class="org.soa.test.activemq.topics.TopicMessageListener1" /> <bean id="topicMessageListener2" class="org.soa.test.activemq.topics.TopicMessageListener2" /> <!-- Topic接收监听(Topic)Topic的第1个监听者 --> <bean id="topicJmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicDestination" /> <property name="messageListener" ref="topicMessageListener1" /> </bean> <!-- Topic接收监听(Topic)Topic的第2个监听者--> <bean id="topicJmsContainer2" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="topicDestination" /> <property name="messageListener" ref="topicMessageListener2" /> </bean> <!-- Topic模式 End --> |
三、队列发送端及测试程序
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package org.soa.test.activemq.topics;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Component;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Session;/** * Created by JamesC on 16-9-22. */@Componentpublic class TopicProvider { @Autowired @Qualifier("jmsTemplateTopic") private JmsTemplate topicJmsTemplate; /** * 向指定的topic发布消息 * * @param topic * @param msg */ public void publish(final Destination topic, final String msg) { topicJmsTemplate.send(topic, new MessageCreator() { public Message createMessage(Session session) throws JMSException { System.out.println("topic name 是" + topic.toString() + ",发布消息内容为:\t" + msg); return session.createTextMessage(msg); } }); }} |
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
//TopicMessageListener1package org.soa.test.activemq.topics;import org.springframework.stereotype.Component;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;/** * Created by JamesC on 16-9-22. */@Componentpublic class TopicMessageListener1 implements MessageListener { public void onMessage(Message message) { TextMessage tm = (TextMessage) message; try { System.out.println("TopicMessageListener_1 \t" + tm.getText()); } catch (JMSException e) { e.printStackTrace(); } }}//TopicMessageListener2package org.soa.test.activemq.topics;import org.springframework.stereotype.Component;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;/** * Created by JamesC on 16-9-22. */@Componentpublic class TopicMessageListener2 implements MessageListener { public void onMessage(Message message) { TextMessage tm = (TextMessage) message; try { System.out.println("TopicMessageListener_2 \t" + tm.getText()); } catch (JMSException e) { e.printStackTrace(); } }} |
3、测试程序
|
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
|
package org.soa.test.activemq.topics;import org.apache.activemq.command.ActiveMQQueue;import org.apache.activemq.command.ActiveMQTopic;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;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 TopicTest { @Autowired private Destination queueDestination; @Autowired private TopicProvider provider; //向默认Topic发消息 @Test public void send() { //坑爹的是:这里不要用ActiveMQQueue,会默认按Queue发送;要使用ActiveMQTopic,按Topic发送 //ActiveMQQueue des = new ActiveMQQueue("topic1"); ActiveMQTopic des = new ActiveMQTopic("topic1"); provider.publish(des,"topic消息示例"); }} |
ActiveMQ_Topic队列(三)的更多相关文章
- SDUT-2133_数据结构实验之栈与队列三:后缀式求值
数据结构实验之栈与队列三:后缀式求值 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运算符的后缀表示式 ...
- java实现单链表、栈、队列三种数据结构
一.单链表 1.在我们数据结构中,单链表非常重要.它里面的数据元素是以结点为单位,每个结点是由数据元素的数据和下一个结点的地址组成,在java集合框架里面 LinkedList.HashMap(数组加 ...
- OpenStack 安装数据库和rabbitmq消息队列 (三)
一)安装配置数据库 1.1.安装包 # yum install mariadb mariadb-server python2-PyMySQL -y 1.2.配置数据库 # vim /etc/my.cn ...
- 数据结构和算法之栈和队列三:自定义一个栈包含min函数
我们都知道一个栈的特点是后进先出,如果我们要实现在O(1)的时间内找到一个栈里面的最小值,我们应该怎么解决?如果我们采用遍历获取的思路那必然所需要的时间是O(N)与我们所需要的要求明显不符合,这时候我 ...
- 数据结构实验之栈与队列三:后缀式求值(SDUT 2133)
题解:把每一步计算的答案再存在栈里面,直到计算结束. 如果是操作数 那么直接入栈:如果是运算符,那么把栈里面最顶部的两个操作数拿出来进行运算,运算结果再放入到栈里面,计算完所有的(#之前的长度位len ...
- Java-五种线程池,四种拒绝策略,三种阻塞队列(转)
Java-五种线程池,四种拒绝策略,三种阻塞队列 三种阻塞队列: BlockingQueue<Runnable> workQueue = null; workQueue = n ...
- 初识Python第三天(二)
2.2 OrderedDict有序字典 import collections dic = collections.OrderedDict() dic['k1'] = 'v1' dic['k2'] = ...
- Linux学习笔记28——消息队列
一 关于消息队列 消息队列提供了一种从一个进程向另一个进程发送一个数据块的方法,而且,每个数据块都被认为含有一个类型,接收进程可以独立地接受含有不同类型值的数据块.可以通过发送消息来几乎完全避免命名管 ...
- ActiveMQ_5死信队列
activemq死信队列 DLQ-死信队列(Dead Letter Queue)用来保存处理失败或者过期的消息. 出现以下情况时,消息会被redelivered: A transacted sessi ...
随机推荐
- python读取excel并制表输出
源码如下: #!/usr/bin/python #coding=UTF-8 import xlrd import sys from texttable import Texttable def she ...
- UVA 12169 Disgruntled Judge【扩展欧几里德】
题意:随机选取x1,a,b,根据公式xi=(a*xi-1+b)%10001得到一个长度为2*n的序列,奇数项作为输入,求偶数项,若有多种,随机输出一组答案. 思路:a和b均未知,可以考虑枚举a和b,时 ...
- 以纯面向对象的JS编写最基本的数据字典案例
之前有讲到过数据字典,什么是数据字典,用来干啥的,这个不细说了,今天来说说如何实现数据字典功能 无非就是维护数据字典,对数据字典对象进行增删改成,曾经我写过一个页面跳转形式的,十分简单,不说了,今天用 ...
- 面试题:return和finally执行
Demo类: public class Demo { public int get() { int x=1; try { x++; return x; }finally{ ++x; } } } Tes ...
- 全息眼镜HoloLens可快速捕捉真人3D图像
http://www.d9soft.com/zixun/62287.html 北京时间3月28日午间消息,微软研发部门开发出一种新的3D视频捕捉系统“Holoportation”,可以实现将某人3D图 ...
- android gridview画分割线,如图:
1.先上图: 2.具体实现代码: public class LineGridView extends GridView { public LineGridView(Context context) { ...
- UIScrollView创建相册
1.设置滚动相册 1.1将存放图片数组传过来,及当前图片的索引 1.2在控制器中创建ScrollView,设置它的contentSize,contentOffset. 1.3通过传过来的图片数组创建U ...
- oracle存储过程中的if...elseif...else用法
if ... then ... elsif ... then ... else ... end if; or if ... then ... else ... end ...
- expect结合ssh遍历线上机器
有个需求,有个文件删除了,但是不确定线上机器还都存不存在 #!/home/work/.jumbo/bin/expect -f set timeout - set mac [lindex $argv ] ...
- VS2010 生成Xml格式的注释文档
项目, 属性, build, 勾选xml document file, 重新build, 即可生成xml注释文件, 然后还得找工具软件(看到anytao推荐SandCastle) 生成更易读的帮助文档 ...