在Spring Boot中集成ActiveMQ相对还是比较简单的,都不需要安装什么服务,默认使用内存的activeMQ,当然配合ActiveMQ Server会更好。在这里我们简单介绍怎么使用,本节主要分以下几个步骤:

(1) 新建Maven Java Project;

(2) 在pom.xml引入依赖;

(3) 编码测试

(4) 配置信息

接下来看看各个步骤的操作:

(1) 新建Maven Java Project;

新建一个工程取名为spring-boot-activemq

(2) 在pom.xml引入依赖;

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.kfit</groupId>

<artifactId>spring-boot-activemq</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>spring-boot-activemq</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!-- jdk版本号,Angel在这里使用1.8,大家修改为大家本地配置的jdk版本号即可 -->

<java.version>1.8</java.version>

</properties>

<!--

spring boot 父节点依赖,

引入这个之后相关的引入就不需要添加version配置,

spring boot会自动选择最合适的版本进行添加。

-->

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.4.0.RELEASE</version>

</parent>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>

<!-- spring boot web支持:mvc,aop... -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- activemq support -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-activemq</artifactId>

</dependency>

</dependencies>

</project>

这里是引入了activemq的依赖,在这里需要注意下spring boot的版本是1.4.0之前的大部分的例子都是1.3.3的,这里必须是1.4+不然是不支持activemq。

(3) 编码测试

这里主要涉及到几个角色,消息生产者,消息队列,消息消费者。所以只需要把这个解决实现了,编码也就完成了。

消息队列Queue,这里编写在启动类App.java中,以@Bean的方式注入:

package com.kfit;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.Bean;

/**

*

* @author Angel --守护天使

* @version v.0.1

* @date 2016年8月23日

*/

@SpringBootApplication

public class App {

@Bean

public Queue queue() {

return new ActiveMQQueue("sample.queue");

}

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

在这里注入了一个ActiveMQQueue。

消息生产者com.kfit.demo.Producer:

package com.kfit.demo;

import javax.jms.Queue;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.scheduling.annotation.EnableScheduling;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

/**

* 消息生产者.

* @author Angel --守护天使

* @version v.0.1

* @date 2016年8月23日

*/

@Component

@EnableScheduling

public class Producer {

@Autowired

private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired

private Queue queue;

@Scheduled(fixedDelay=3000)//每3s执行1次

public void send() {

this.jmsMessagingTemplate.convertAndSend(this.queue, "hi,activeMQ");

}

}

这里使用JmsMessagingTemplate  进行消息的操作,然后使用任务调度3秒1次执行消息的发布。

消息消费者com.kfit.demo.Consumer:

package com.kfit.demo;

import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;

/**

* 消息消费者.

* @author Angel --守护天使

* @version v.0.1

* @date 2016年8月23日

*/

@Component

public class Consumer {

@JmsListener(destination = "sample.queue")

public void receiveQueue(String text) {

System.out.println(text);

}

}

这里主要是加入了@JmsListener进行监听,然后接收消息然后打印。

好了,到这里就大功告成了。运行下程序观察控制台的打印信息:

hi,activeMQ

hi,activeMQ

hi,activeMQ

(4) 配置信息

在上面我们并没有配置activeMQ的相关信息,实际上spring boot提供了默认的配置,我们可以在application.properties进行配置:

# ACTIVEMQ (ActiveMQProperties)

spring.activemq.broker-url= # URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`

spring.activemq.in-memory=true # Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified.

spring.activemq.password= # Login password of the broker.

spring.activemq.user= # Login user of the broker.

spring.activemq.packages.trust-all=false # Trust all packages.

spring.activemq.packages.trusted= # Comma-separated list of specific packages to trust (when not trusting all packages).

spring.activemq.pool.configuration.*= # See PooledConnectionFactory.

spring.activemq.pool.enabled=false # Whether a PooledConnectionFactory should be created instead of a regular ConnectionFactory.

spring.activemq.pool.expiry-timeout=0 # Connection expiration timeout in milliseconds.

spring.activemq.pool.idle-timeout=30000 # Connection idle timeout in milliseconds.

spring.activemq.pool.max-connections=1 # Maximum number of pooled connections.

86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】的更多相关文章

  1. 63.JPA/Hibernate/Spring Data概念【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 事情的起源,无意当中在一个群里看到这么一句描述:"有人么?默默的问一句,现在开发用mybatis还是hibernate还是jpa&quo ...

  2. 47. Spring Boot发送邮件【从零开始学Spring Boot】

    (提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...

  3. 20. Spring Boot Servlet【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52069482 Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可 ...

  4. (20)Spring Boot Servlet【从零开始学Spring Boot】

    Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet.Filter.Listener.Interceptor 等等. 当使用Spring-Boot时,嵌 ...

  5. 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 针对文章: ()Spring Boot 添加JSP支持[从零开始学Spring Boot] 有网友提了这么一些疑问: 1.Spring Boot使用jsp时,仍旧可以打成 ...

  6. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  7. 57. Spring 自定义properties升级篇【从零开始学Spring Boot】

    之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...

  8. 17、Spring Boot普通类调用bean【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52013017 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个 ...

  9. 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...

  10. 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】

    [原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...

随机推荐

  1. jQuery3.2.1 和2.0和 1区别

    1. 移除旧的IE工作区新的最终版最主要的目标是更加快速,更加时尚,因此,那些支持早于IE9版本的相关技术与工作区都被移除了.这意味着如果你想要或者需要支持IE6-8,你必须用回1.12版本,因为甚至 ...

  2. iOS Category实现原理 (补充)

    iOS Category实现原理 (补充) load 和 initialize load load方法会在程序启动就会调用,当装载类信息的时候就会调用. 调用顺序看一下源代码.在 objc-loadm ...

  3. NBUT 1115 Cirno's Trick (水)

    题意: 给出多个double数,去掉其最小的和最大的,再对余下的求均值. 思路: 再输入时将最大和最小去掉,顺便统计非最值的和,输出时除一下个数即可. #include <bits/stdc++ ...

  4. MINST手写数字识别(一)—— 全连接网络

    这是一个简单快速入门教程——用Keras搭建神经网络实现手写数字识别,它大部分基于Keras的源代码示例 minst_mlp.py. 1.安装依赖库 首先,你需要安装最近版本的Python,再加上一些 ...

  5. spfa模板+讲解

    zz http://blog.sina.com.cn/s/blog_6ad20aef0100mc1a.html Spfa算法 (模板源代码) 这是Bellman Ford的改进算法.    算法介绍: ...

  6. robotframework接口测试实例

    *** Settings *** Library Collections Library RequestsLibrary *** Test Cases *** test Create Session ...

  7. thinkphp 结合phpexcel实现excel导入

    控制器文件: class ExcelAction extends Action { public function __construct() { import('ORG.Util.ExcelToAr ...

  8. java在线聊天项目1.1版 ——开启多个客户端,分别实现注册和登录功能,使用客户端与服务端信息request机制,重构线程,将单独的登录和注册线程合并

    实现效果图: eclipse项目中初步整合之前的各个客户端和服务端的窗口与工具类,效果如下图: 已将注册服务器线程RegServer功能放到LoginServer中,使用客户端与服务端的request ...

  9. Mysql 5.7在Linux上部署及远程访问

    序言:最近要和伙伴一起组队,做.NET Core项目.所以自己就租了一个阿里云服务器,并且装了Linux和MySQL.这里面我的Linux是CentOs 7. 第一步 添加Mysql Yum库 这里面 ...

  10. 利用Resttemplate进行put请求

    开发中,最常用的是post.get这两种.今天我给大家展示一个利用put请求的demo,其实put请求跟post请求没啥区别,但是没有返回值. void put(String var1, @Nulla ...