ActiveMQ整合Spring入门用法解析

一.ActiveMQ整合Spring基础

  ActiveMQ和Spring的整合,其实是把activemq的一些对象交给spring来管理,比如连接工厂,queue,top等等

二.依赖

  除了activemq本身提供的jar包外,还需要两个spring整合activemq的jar:

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
    </dependency>
    <!-- activemq -->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-all</artifactId>
    </dependency>

三.在spring配置文件中,配置activemq相关的对象

  》生产者需要配置的对象:activemq原生连接工厂,spring包装过的工厂,JmsTemplate(生产者),queue,topic

<!-- 配置activemq的连接工厂 -->
<bean name="activemqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  <constructor-arg index="0" value="tcp://192.168.xx.xxx:61616"></constructor-arg>
</bean>

<!-- 配置spring包装的连接工厂 -->
<bean name="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
  <!-- 设置activemq的连接工厂 -->
  <property name="targetConnectionFactory" ref="activemqConnectionFactory"></property>
</bean>

<!-- 配置生产者对象:jmstemplate -->
<bean name="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  <!-- 设置工厂 -->
  <!-- <constructor-arg name="connectionFactory" ref="connectionFactory"></constructor-arg> -->
  <property name="connectionFactory" ref="connectionFactory"></property>
</bean>

<!-- 配置queue对象 -->
<bean name="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
  <constructor-arg name="name" value="spring-active-queue"></constructor-arg>
</bean>

<!-- 配置topic对象 -->
<bean name="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
  <constructor-arg name="name" value="spring-active-topic"></constructor-arg>
</bean>

  测试代码:

//测试生产者发生queue
  @Test
  public void testJMSTemplateByQueue() throws Exception {

    //创建spring容器
    ApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");

    //获取jmstemplate
    JmsTemplate jmsTemplate = ioc.getBean(JmsTemplate.class);

    //获取目的对象
    Destination queue = (Destination) ioc.getBean("queueDestination");

    //发送消息,并设置目的对象和消息
    jmsTemplate.send(queue,new MessageCreator() {

      public Message createMessage(Session session) throws JMSException {

        TextMessage message = session.createTextMessage();

        message.setText("hello spring activemq");

        return message;
      }
    });

  小结:生产者是需要在特定的时间发送指定的消息内容,因此是需要书写代码的

  》消费者需要配置的对象:activemq原生连接工厂,spring包装过的工厂,监听容器(消费者),监听器,queue,topic

  》之前监听器我们是直接实现匿名内部类的方式,但是在spring这就不能了,还是书写一个实现类并继承messageListener接口

<!-- 配置activemq的连接工厂 -->
<bean name="activemqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
  <constructor-arg index="0" value="tcp://192.168.xx.xxx:61616"></constructor-arg>
</bean>

<!-- 配置spring包装的连接工厂 -->
<bean name="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
  <!-- 设置activemq的连接工厂 -->
  <property name="targetConnectionFactory" ref="activemqConnectionFactory"></property>
</bean>

<!-- 配置监听器 -->
<bean name="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener"></bean>

<!-- 配置监听容器:消费者 -->
<bean name="messageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  <!-- 设置连接工厂 -->
  <property name="connectionFactory" ref="connectionFactory"></property>
  <!-- 设置目的对象 -->
  <property name="destination" ref="queueDestination"></property>
  <!-- 设置监听器 -->
  <property name="messageListener" ref="myMessageListener"></property>
</bean>

<!-- 配置queue对象 -->
<bean name="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
  <constructor-arg name="name" value="spring-active-queue"></constructor-arg>
</bean>

<!-- 配置topic对象 -->
<bean name="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
  <constructor-arg name="name" value="spring-active-topic"></constructor-arg>
</bean>

  测试代码:

@Test
  public void testListener() throws IOException {
    ApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
    System.in.read();
  }

  小结:我们的测试代码很简单,只要把容器启动了,监听器就会一致监听着。不需要写任何代码,监听器只要一直关注目的对象Destination是否有消息发送过来

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 浅谈Spring Boot 整合ActiveMQ的过程

    RabbitMQ是比较常用的AMQP实现,这篇文章是一个简单的Spring boot整合RabbitMQ的教程. 安装ActiveMQ服务器,(也可以不安装,如果不安装,会使用内存mq) 构建Spring boot项目,增加依赖项,只需要添加这一项即可 <!-- 添加acitivemq依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring

  • Springboot整合activemq的方法步骤

    今天呢心血来潮,也有很多以前的学弟问到我关于消息队列的一些问题,有个刚入门,有的有问题都来问我,那么今天来说说如何快速入门mq. 一.首先说下什么是消息队列? 1.消息队列是在消息的传输过程中保存消息的容器. 二.为什么要用到消息队列? 主要原因是由于在高并发环境下,由于来不及同步处理,请求往往会发生堵塞,比如说,大量的insert,update之类的请求同时到达 MySQL ,直接导致无数的行锁表锁,甚至最后请求会堆积过多,从而触发too many connections错误.通过使用消息队列

  • 详解Java消息队列-Spring整合ActiveMq

    1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 1.消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Java 实现. 2.优势:异步.可靠 3.消息模型:点对点,发布/订阅 4.JMS中的对象 然后在另一篇博客<Java消息队列-ActiveMq实战>中,和大家一起从0到1的开启了一个ActiveMq 的项目,在项目开发的过程中,我们对ActiveMq有了一定的了解: 1.多种语言和协议编写客户端

  • SpringBoot整合ActiveMQ过程解析

    目录结构 引入 maven依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> </parent> <properties> &l

  • 详解Springboot整合ActiveMQ(Queue和Topic两种模式)

    写在前面: 从2018年底开始学习SpringBoot,也用SpringBoot写过一些项目.这里对学习Springboot的一些知识总结记录一下.如果你也在学习SpringBoot,可以关注我,一起学习,一起进步. ActiveMQ简介 1.ActiveMQ简介 Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件:由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行. 2.ActiveMQ下载 下载地址:htt

  • spring整合JMS实现同步收发消息(基于ActiveMQ的实现)

    本文介绍了spring整合JMS实现同步收发消息(基于ActiveMQ的实现),分享给大家,具体如下: 1. 安装ActiveMQ 注意:JDK版本需要1.7及以上才行 到Apache官方网站下载最新的ActiveMQ的安装包,并解压到本地目录下,下载链接如下:http://activemq.apache.org/download.html,解压后的目录结构如下: bin目录结构如下: 如果我们是32位的机器,就双击win32目录下的activemq.bat,如果是64位机器,则双击win64目

  • activemq整合springboot使用方法(个人微信小程序用)

    主题 ActiveMQ Spring Boot 小程序开发 1.引入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath /> <!-- lookup

  • 详解spring boot整合JMS(ActiveMQ实现)

    本文介绍了spring boot整合JMS(ActiveMQ实现),分享给大家,也给自己留个学习笔记. 一.安装ActiveMQ 具体的安装步骤,请参考我的另一篇文章:http://www.jb51.net/article/127117.htm 二.新建spring boot工程,并加入JMS(ActiveMQ)依赖 三.工程结构 pom依赖如下: <?xml version="1.0" encoding="UTF-8"?> <project xm

  • ActiveMQ整合Spring入门用法解析

    一.ActiveMQ整合Spring基础 ActiveMQ和Spring的整合,其实是把activemq的一些对象交给spring来管理,比如连接工厂,queue,top等等 二.依赖 除了activemq本身提供的jar包外,还需要两个spring整合activemq的jar: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId&

  • Spring jpa和mybatis整合遇到的问题解析

    前一阵子接手了一个使用SpringBoot 和spring-data-jpa开发的项目,后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查询写起来也比较费劲,所以便加入了mybatis的支持 开始的时候 @Configuration @EnableJpaRepositories("com.xxx.xxx.repository") class JpaConfig 使用这种方式去配置的jpa,遇到一个问题,就是能select 但是不能save,所以就修改为配置文件的方式

  • Spring整合MyBatis图示过程解析

    这篇文章主要介绍了Spring整合MyBatis图示过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导入所需要的jar依赖 !--MyBatis和Spring的整合包 由MyBatis提供--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <vers

  • Spring Boot 整合 Shiro+Thymeleaf过程解析

    这篇文章主要介绍了Spring Boot 整合 Shiro+Thymeleaf过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导包 <!-- springboot 与 shiro 的集成--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <

  • Spring Boot整合Spring Cache及Redis过程解析

    这篇文章主要介绍了Spring Boot整合Spring Cache及Redis过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.安装redis a.由于官方是没有Windows版的,所以我们需要下载微软开发的redis,网址: https://github.com/MicrosoftArchive/redis/releases b.解压后,在redis根目录打开cmd界面,输入:redis-server.exe redis.wind

  • sentinel 整合spring cloud限流的过程解析

    spring cloud基于http进行服务调用,大致过程如下: 服务提供端:提供http接口,并向服务中心注册服务信息 服务消费端:将服务端的http接口作为本地服务,从注册中心读取服务提供端信息,使用feign发起远程调用 相关依赖 <!-- 服务注册与发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibab

  • Spring Boot整合Spring Data JPA过程解析

    Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> &l

  • Spring Boot整合Spring Security的示例代码

    本文讲述Spring Boot整合Spring Security在方法上使用注解实现权限控制,使用自定义UserDetailService,从MySQL中加载用户信息.使用Security自带的MD5加密,对用户密码进行加密.页面模板采用thymeleaf引擎. 源码地址:https://github.com/li5454yong/springboot-security.git 1.引入pom依赖 <parent> <groupId>org.springframework.boot

  • Java设计模式模板方法模式(Template)用法解析

    这篇文章主要介绍了Java设计模式模板方法模式(Template)用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 前言: 我们在开发中有很多固定的流程,这些流程有很多步凑是固定的,比如JDBC中获取连接,关闭连接这些流程是固定不变的,变动的只有设置参数,解析结果集这些是根据不同的实体对象"来做调整",针对这种拥有固定算法流程,其中有固定的步凑,存在不固定的步凑的情况下就诞生了模板方法模式. 模板方法模式(Template)定义

  • Springboot整合通用mapper过程解析

    这篇文章主要介绍了springboot整合通用mapper过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 找到springboot工程下的pom.xml文件,导入如下的依赖jar包 <!--配置通用Mapper start--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starte

随机推荐