在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. JAVA-汉字转换成汉语拼音(pinyin4j-2.5.0-sources.jar)

    在项目中,经常会使用汉语拼音,特别是把汉字转换成汉语拼音.下面给出一种常用的工具. 在使用该程序必须添加 pinyin4j-2.5.0-sources.jar包. import net.sourcef ...

  2. 关于如何用jq定位到某个元素的索引

    在点击事件触发时候,通常如果有同样的className的列表我们都需获取它的索引来知道到底点击的是那一个 $('.info_content').mousemove(function(){ var ro ...

  3. Scanner-String-StringBuilder-API

    1.能够明确API的使用步骤     1)打开帮助文档     2)点击显示,找到索引,看到输入框     3)你要找谁?在输入框里输入,然后回车     4)看包:java.lang下的类不需 ...

  4. PHP-PHPExcel用法详解

    以下文章来源:diandian_520 http://blog.csdn.net/diandian_520/article/details/7827038 1.header header(" ...

  5. vue+element ui项目总结点(三)富文本编辑器 vue-wangeditor

    1.参考 https://www.npmjs.com/package/vue-wangeditor 使用该富文本编辑器 <template> <div class="egi ...

  6. CSS的相对定位和绝对定位

     relative的意思就是相对自己的一开始的位置进行的定位.如图: 但是这个元素的本身边距不变,还在原来位置   absolute的意思就是 如果它的父元素设置了除static之外的定位,比如pos ...

  7. 多源最短路径 – Floyd-Warshall Algorithm

    介绍: 是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权(但不可存在负权回路)的最短路径问题,同时也被用于计算有向图的传递闭包. Floyd-Warshall算法的时间复杂度是O(N3) ...

  8. 快学UiAutomator UiDevice API 详解

    一.按键使用 返回值 方法名 说明 boolean pressBack() 模拟短按返回back键 boolean pressDPadCenter() 模拟按轨迹球中点按键 boolean press ...

  9. applicationContext.xml重要配置

    <!-- 加载 hibernate.properties 文件--> <bean id="propertyConfig" class="org.spri ...

  10. hihoCoder-1097-Prim

    这题就是prim的板子题,不过如果用end每次初始化为-1的话,我们就不需要对于每次选中的下一个点进行判断是否选中了,因为每次外层循环第一次进入都是可以的. 然后还很 (i=1:i<=n;i++ ...