一直在赶项目,好久没有写博文了,中间偶尔有些代码什么的,也都是放到github了,不过大多都是测试代码,毕竟有些成型的东西是给公司写的,鉴于职业道德,还是不好公开。

言归正传,这两天在接入第三方的收费管理系统,后台有个扫描记录的,数据然后发送到我这里,然后我来处理。

毕竟是走的restful的方式,说到底是比较多的http请求,他们不关心结果,只知道通信成功即可。于是,又用到了消息队列。前面用的是activemq,这里需要再配置一个队列。

如果不整合spring,用原生的activemq代码来写的话,多个队列很容易实现。但是整合spring后,大多都是配置一个队列。琢磨了一下,这里配置了两个队列,其实就是一个配置文件。因为是测试,这样写的有点不雅观。

配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 消息中介-->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616?wireFormat.maxInactivityDuration=0"/>
<property name="useAsyncSend" value="true" />
</bean> <!-- 队列目的地-->
<bean id="myQueueOne" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="QueueDemoOne"/>
</bean>
<bean id="myQueueTwo" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="QueueDemoTwo" />
</bean> <bean id="jmsTemplateOne" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 设置默认的消息目的地-->
<property name="defaultDestination" ref="myQueueOne"/>
<property name="deliveryMode" value="1"></property>
<!-- 消息不持久化 -->
<property name="explicitQosEnabled" value="true"></property>
<!-- 由于receiver方法是同步的,所以我们在这里对接收设置超时时间-->
<!-- <property name="receiveTimeout" value="60000"/> -->
</bean>
<bean id="jmsTemplateTwo" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- 设置默认的消息目的地-->
<property name="defaultDestination" ref="myQueueTwo"/>
<property name="deliveryMode" value="1"></property>
<!-- 消息不持久化 -->
<property name="explicitQosEnabled" value="true"></property>
</bean> <!-- 消息发送者-->
<bean id="producer1" class="com.ww.topic.SenderOne">
<property name="jmsTemplate" ref="jmsTemplateOne"/>
<!-- 消息目的地,因为jmsTemplate有默认的了,所以这里可以省略
<property name="destination" ref=" myQueue "/>-->
</bean>
<bean id="producer2" class="com.ww.topic.SenderTwo">
<property name="jmsTemplate" ref="jmsTemplateTwo"/>
<!-- 消息目的地,因为jmsTemplate有默认的了,所以这里可以省略
<property name="destination" ref=" myQueue "/>-->
</bean> <!-- 消息接收监听器用于异步接收消息-->
<bean id="container1" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="myQueueOne"/>
<property name="messageListener" ref="jmsListenerOne"/>
</bean>
<bean id="container2" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destination" ref="myQueueTwo"/>
<property name="messageListener" ref="jmsListenerTwo"/>
</bean> <!-- 消息监听实现方法一 -->
<bean id="jmsListenerOne" class="com.ww.topic.ListenerOne">
</bean>
<bean id="jmsListenerTwo" class="com.ww.topic.ListenerTwo">
</bean> </beans>

其实是配置了两个监听器,然后把各自的destination ,sender,listener配置了两个。

测试代码:

sender:

package com.ww.topic;

import org.junit.Test;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator; import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; /**
* Created by wang on 15-3-24.
*/
public class SenderOne {
private JmsTemplate jmsTemplate; public JmsTemplate getJmsTemplate() {
return jmsTemplate;
} public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
} @Test
public void sendQueue(){
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage();
message.setText("This is one sender");
return message;
}
});
}
}

listener:

package com.ww.topic;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; /**
* Created by wang on 15-3-24.
*/
public class ListenerOne implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
try {
System.out.println("REC: "+msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}

第二个和第一个是同理的代码。

源码下载:https://github.com/ThinkCats/Queue

ActiveMq 配置多队列的更多相关文章

  1. 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)

    你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...

  2. 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)

    从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...

  3. zookeeper + LevelDB + ActiveMQ实现消息队列高可用

    通过集群实现消息队列高可用. 消息队列在项目中存储订单.邮件通知.数据分发等重要信息,故对消息队列稳定可用性有高要求. 现在通过zookeeper选取activemq leader的形式实现当某个ac ...

  4. ActiveMQ配置文档

    本文介绍一对一.一对多.持久化.非持久化消息配置方式 一.创建项目 导入jar 二.创建MQ.xml <!-- 配置JMS连接工厂 --> <bean id="connec ...

  5. ACTIVEMQ主题、队列设置用户名密码

    修改文件%ACTIVEMQ_BASE%/conf/activemq.xml,用户名密码储存在文件%ACTIVEMQ_BASE%/conf/credentials.properties中, active ...

  6. Spring集成ActiveMQ配置 --转

    转自:http://suhuanzheng7784877.iteye.com/blog/969865 集成环境 Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可 ...

  7. activeMq 配置(一)

    基础知识补充 1.ActiveMQ从入门到精通(一)https://www.jianshu.com/p/ecdc6eab554c 2.ActiveMQ从入门到精通(二)https://www.jian ...

  8. Spring和ActiveMQ集成实现队列消息以及PUB/SUB模型

    前言:本文是基于Spring和ActiveMQ的一个示例文章,包括了Point-To-Point的异步队列消息和PUB/SUB(发布/订阅)模型,只是做了比较简单的实现,无任何业务方面的东西,作为一个 ...

  9. activemq 的延迟队列和幂等性检查

    一. 延迟消息队列 1. 在提交支付之后,可以发送一个延迟检查的队列,来主动查询用户在支付宝上的支付状态 在mq的配置/config/activeMq.xml的broker实例上配置 schedule ...

随机推荐

  1. for循环计算阶乘

    int x = 10; for(int y = x - 1; y >= 1; y--) { x = x * y; } System.out.println(x); 从10乘到1的阶乘写法. lo ...

  2. Required String parameter ' ' is not present

    Required String parameter ' ' is not present 报错原因: url中的参数错误. 解决方法: 1.修正url中的参数的值. 2.在Controller层中的@ ...

  3. 38. Count and Say (String; DP)

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  4. Android网络类型判断(2g、3g、wifi)

    判断网络类型是wifi,还是3G,还是2G网络,对不同 的网络进行不同的处理,现将判断方法整理给大家,以供参考   说明:下面用到的数据移动2G,联通2G,联通3G,wifi我都已经测试过,暂时手上 ...

  5. memcache简单操作

    <?php $m = new Memcache(); $m->connect('localhost',11211); //获取版本 echo "server's version: ...

  6. How to Check if Linux (Ubuntu, Fedora Redhat, CentOS) is 32-bit or 64-bit

    The number of CPU instruction sets has kept growing, and likewise for the operating systems which ar ...

  7. OpenGLES.Functions.Missing.in.OpenGLES1.x

    转载自: http://maniacdev.com/2009/05/big-list-of-opengl-functions-missing-in-iphone-opengl-es The funct ...

  8. 03 解析库之Beautifulsoup模块

    Beautifulsoup模块   一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 ...

  9. Vue.js 登录注册实现

    转载 http://www.jb51.net/article/118003.htm

  10. 设计模式之flyweight享元模式

    运用共享技术支持大量细粒度对象的使用 Flyweight模式(享元) Java深入到一定程度,就不可避免的碰到设计模式这一概念,了解设计模式,将使自己对java中的接口或抽象类应用有更深的理解.设计模 ...