`

86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

 
阅读更多

 à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

 

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 2016823

 */

@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 2016823

 */

@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  进行消息的操作,然后使用任务调度31次执行消息的发布。

       消息消费者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 2016823

 */

@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.

 

 à悟空学院:https://t.cn/Rg3fKJD

学院中有Spring Boot相关的课程!点击「阅读原文」进行查看!

SpringBoot视频:http://t.cn/A6ZagYTi

Spring Cloud视频:http://t.cn/A6ZagxSR

SpringBoot Shiro视频:http://t.cn/A6Zag7IV

SpringBoot交流平台:https://t.cn/R3QDhU0

SpringData和JPA视频:http://t.cn/A6Zad1OH

SpringSecurity5.0视频:http://t.cn/A6ZadMBe

Sharding-JDBC分库分表实战http://t.cn/A6ZarrqS

分布式事务解决方案「手写代码」:http://t.cn/A6ZaBnIr

分享到:
评论
8 楼 林祥纤 2017-04-27  
z673182 写道
我可以写进去为什么读不出来


订阅正确否?
7 楼 z673182 2017-04-26  
我可以写进去为什么读不出来
6 楼 农村外出务工男JAVA 2017-02-05  
Field mqProducerService in com.tanjie.spring.boot.jms_13.MqController required a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' that could not be found.
- Bean method 'jmsMessagingTemplate' not loaded because Ancestor org.springframework.boot.autoconfigure.jms.JmsAutoConfiguration did not match


Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.jms.core.JmsMessagingTemplate' in your configuration.

你好,按你的方式,报上面的错
5 楼 林祥纤 2016-12-20  
为了吃方便面 写道
问个问题,如果我需要多个queue,应该怎么交给spring应该怎么写。

这种方法应该是只提供了一个queue和对应的生产者和消费者。



这句代码是可以传入一个queue的:

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

主要是确保ActiveMQQueue("sample.queue");
这里的名称不一样,就是不同的queue了。

4 楼 为了吃方便面 2016-12-19  
问个问题,如果我需要多个queue,应该怎么交给spring应该怎么写。

这种方法应该是只提供了一个queue和对应的生产者和消费者。
3 楼 林祥纤 2016-08-26  
焦志广 写道
可以了, 原来是spring.activemq.broker-url=tcp://192.168.0.28:61616 后面多了个空格 ,谢谢你的博客


嗯.
2 楼 焦志广 2016-08-25  
可以了, 原来是spring.activemq.broker-url=tcp://192.168.0.28:61616 后面多了个空格 ,谢谢你的博客
1 楼 焦志广 2016-08-25  
配置远程activemq server 的时候, Error creating bean with name 'jmsMessagingTemplate'

相关推荐

Global site tag (gtag.js) - Google Analytics