activemq的相关使用

一、好言

万物皆有裂缝处,那时光射进来的地方。

二、背景

这篇主要是amq的一些用法,比如队列queue,topic,还有注解的使用,并且看了公司的一些封装,有点感触,在这里面也会说下。

三、内容

3.1 、xml对列的配置方式,queueDestination的配置使用的是对象,在对象中再指定名字:

    <!-- 定义消息队列(Queue) -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 设置消息队列的名字 -->
        <constructor-arg>
            <value>mouse-one</value>
        </constructor-arg>
    </bean>

    <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="queueDestination" />
        <property name="receiveTimeout" value="10000" />
    </bean>
         <!-- 定义消息队列(Queue),我们监听一个新的队列,queue2 -->
         <bean id="queueDestination2" class="org.apache.activemq.command.ActiveMQQueue">
             <!-- 设置消息队列的名字 -->
             <constructor-arg>
                 <value>mouse-two</value>
             </constructor-arg>
         </bean>
         <!-- 配置消息队列监听者(Queue),代码下面给出,只有一个onMessage方法 -->
         <bean id="queueMessageListener" class="com.mouse.moon.message.listener.QueueMessageListener" />

         <!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue2,监听器是上面定义的监听器 -->
         <bean id="jmsContainer"
             class="org.springframework.jms.listener.DefaultMessageListenerContainer">
             <property name="connectionFactory" ref="connectionFactory" />
             <property name="destination" ref="queueDestination2" />
             <property name="messageListener" ref="queueMessageListener" />
         </bean>

amq中,jmsTemplate的send可以接手对象ActiveMQQueue,也可以直接接收DestinationName,所以下面给出配置

3.2、使用名字进行消息推送

  <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestinationName" value="mouse-name-one"></property>
        <property name="receiveTimeout" value="10000" />
    </bean>


         <!-- 配置消息队列监听者(Queue),代码下面给出,只有一个onMessage方法 -->
         <bean id="queueMessageListener" class="com.mouse.moon.message.listener.QueueMessageListener" />

         <!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue2,监听器是上面定义的监听器 -->
         <bean id="jmsContainer"
             class="org.springframework.jms.listener.DefaultMessageListenerContainer" abstract="true">
             <property name="connectionFactory" ref="connectionFactory" />
         </bean>
        <bean parent="jmsContainer">
            <property name="destinationName" value="mouse-name-two" />
            <property name="messageListener" ref="queueMessageListener" />
        </bean>

junit代码:

@Test
    public void testProduce(){
        for(int i = 0;i < 5; i++){
            producerService.sendMessage("mouse-name-one","测试消息发送了哈哈哈哈哈哈哈" + i+new Date()+new Random().nextInt(100));
        }
    }

这里直接调用封装好的对象,传入名字进行。
代码都有,可以直接看配置了。

3.3 以注解的形式配置监听器
启用注解

<jms:annotation-driven/>
<bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
          <property name="connectionFactory" ref="connectionFactory"/>
</bean>
package com.mouse.moon.message.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * Created by Mahone Wu on 2017/4/11.
 */
@Component
public class QueueMessageListenerAnno extends MessageListenerAdapter {
    private Logger logger = LoggerFactory.getLogger(QueueMessageListenerAnno.class);

    @JmsListener(destination = "mouse-one",concurrency = "5-10")
    public void onMessageMahone(Message message, Session session){
        TextMessage tm = (TextMessage)message;
        try{
            logger.info("注解QueueMessageListenerAnno 收到消息{}",tm.getText());
        }catch (JMSException e){
            logger.error("监听的bug"+e);
            e.printStackTrace();
        }
    }
}
图片.png

3.4、有关公用提取

<!-- 消息监听容器(Queue),配置连接工厂,监听的队列是queue2,监听器是上面定义的监听器 -->
<bean id="jmsContainer"
             class="org.springframework.jms.listener.DefaultMessageListenerContainer" abstract="true">
     <property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean parent="jmsContainer">
   <property name="destinationName" value="mouse-name-two" />
   <property name="messageListener" ref="queueMessageListener" />
</bean>

可以看出下面这个bean我使用了属性parent,这里使用了父类,也就是类似说继承一些属性,并且jmsContainer中的abstract属性我设置为了true,否则会报错,因为其他几个属性都是必填项目,abstract属性为true后则抽象了。这样,我们在使用XML时候不用每一个这样的配置都要指定connectionFactory属性。
上述配置有借鉴使用Spring JMS收发消息该文章。

我自己的代码地址在GitHub上可见
https://github.com/MahoneWu/mq

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,019评论 25 708
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,742评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,117评论 6 13
  • 1、 我有个同事叫小玲,名字虽然土了点,但人长的真不错,性格也不差。 这一天,她找我吐槽,说她一直被家人逼婚。 这...
    赵凌84阅读 804评论 0 1