springboot获取active_86. Spring Boot集成ActiveMQ【从零开始学Spring Boot】

13753cc92affea4c294ee8f6dd76971c.png

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

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

Sharding-JDBC分库分表实战:

在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引入依赖;

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

4.0.0

com.kfit

spring-boot-activemq

0.0.1-SNAPSHOT

jar

spring-boot-activemq

http://maven.apache.org

UTF-8

1.8

org.springframework.boot

spring-boot-starter-parent

1.4.0.RELEASE

junit

junit

test

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-activemq

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

(3)编码测试

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

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

packagecom.kfit;

importjavax.jms.Queue;

importorg.apache.activemq.command.ActiveMQQueue;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.context.annotation.Bean;

/**

*

*@authorAngel--守护天使

*@versionv.0.1

*@date2016年8月23日

*/

@SpringBootApplication

publicclassApp {

@Bean

publicQueue queue() {

returnnewActiveMQQueue("sample.queue");

}

publicstaticvoidmain(String[]args) {

SpringApplication.run(App.class,args);

}

}

在这里注入了一个ActiveMQQueue。

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

packagecom.kfit.demo;

importjavax.jms.Queue;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.jms.core.JmsMessagingTemplate;

importorg.springframework.scheduling.annotation.EnableScheduling;

importorg.springframework.scheduling.annotation.Scheduled;

importorg.springframework.stereotype.Component;

/**

*消息生产者.

*@authorAngel--守护天使

*@versionv.0.1

*@date2016年8月23日

*/

@Component

@EnableScheduling

publicclassProducer {

@Autowired

privateJmsMessagingTemplatejmsMessagingTemplate;

@Autowired

privateQueuequeue;

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

publicvoidsend() {

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

}

}

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

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

packagecom.kfit.demo;

importorg.springframework.jms.annotation.JmsListener;

importorg.springframework.stereotype.Component;

/**

*消息消费者.

*@authorAngel--守护天使

*@versionv.0.1

*@date2016年8月23日

*/

@Component

publicclassConsumer {

@JmsListener(destination ="sample.queue")

publicvoidreceiveQueue(Stringtext) {

System.out.println(text);

}

}

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

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

hi,activeMQ

hi,activeMQ

hi,activeMQ

(4)配置信息

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

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.

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

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

Sharding-JDBC分库分表实战:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值