以ActiveMQ为例JAVA消息中间件学习【3】——SpringBoot中使用ActiveMQ
前言
首先我们在java环境中使用了ActiveMQ,然后我们又在Spring中使用了ActiveMQ
本来这样已经可以了,但是最近SpringBoot也来了。所以在其中也需要使用试试。
可以提前透露的时候,在SpringBoot使用是最简单的一种
导入依赖
在原有SpringBoot项目的依赖加入下面
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
配置ActiveMQ连接信息
我是用的.yml文件进行配置,application.yml配置
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
队列模式消息发送配置
import
org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Service; import javax.jms.Destination; /**
* 发送消息
*/
@Service(
"producerService") public class ProducerService { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; public void sendMessage(String message){ Destination destination = new ActiveMQQueue("queue-test"); jmsMessagingTemplate.convertAndSend(destination, message); System.out.println("发送消息:" +
message);
} }
单元测试
import
org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* 发送消息单元测试
*/
@RunWith(SpringJUnit4ClassRunner.class
) @SpringBootTest public class ProducerServiceTest { @Autowired private ProducerService producerService; @Test public void sendMessage() throws Exception { producerService.sendMessage("测试消息"
);
} }
然后运行单元测试进入http://localhost:8161查看队列信息你就会发现,消息已经发送了。
是不是很惊讶,这样就已经全部配置好了,没有之前在spring中那些其他的配置,超清晰。
消息消费者配置
import
org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Service; /**
* 消息的消费者
*/
@Service(
"consumerService") public class ConsumerService { @JmsListener(destination = "queue-test") public void receiveQueue(String text) { System.out.println("收到消息:" +
text);
} }
是不是更简单了
直接启动SpringBoot就能收到刚才发送的消息了
主题模式使用
SpringBoot默认使用的是队列模式,如果要使用主题模式需要修改application.yml
spring: jms:
pub-sub-domain: true
然后需要修改消息发送者代码中:Destination destination = new ActiveMQTopic("topic-test");
消息消费者代码中:@JmsListener(destination = "topic-test")
可惜的是,这样又只能使用主题模式了,队列模式不能使用了那么怎么同时使用两种模式呢?
同时使用两种模式
首先新建JMS配置类
import
org.apache.activemq.command.ActiveMQQueue; import org.apache.activemq.command.ActiveMQTopic; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.JmsListenerContainerFactory; import javax.jms.ConnectionFactory; import javax.jms.Queue; import javax.jms.Topic; /**
* JMS配置
*/
@Configuration
public class JmsConfig { public final static String TOPIC = "topic-test"; public final static String QUEUE = "queue-test"; @Bean public Queue queue() { return new ActiveMQQueue(QUEUE); } @Bean public Topic topic() { return new ActiveMQTopic(TOPIC); } @Bean public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory(); bean.setPubSubDomain(true); bean.setConnectionFactory(activeMQConnectionFactory); return bean; } @Bean public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory(); bean.setConnectionFactory(activeMQConnectionFactory); return
bean;
} }
消息的发送者
/**
* 发送消息
*/
@Service(
"producerService") public class ProducerService { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Autowired private Topic topic; public void sendMessage(String message) { jmsMessagingTemplate.convertAndSend(queue, message); jmsMessagingTemplate.convertAndSend(topic, message); System.out.println("发送消息:" +
message);
} }
消息的消费者
import
org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Service; /**
* 消息的消费者
*/
@Service(
"consumerService") public class ConsumerService { @JmsListener(destination = JmsConfig.TOPIC, containerFactory = "jmsListenerContainerTopic") public void onTopicMessage(String msg) { System.out.println("接收到topic消息:" + msg); } @JmsListener(destination = JmsConfig.QUEUE, containerFactory = "jmsListenerContainerQueue") public void onQueueMessage(String msg) { System.out.println("接收到queue消息:" +
msg);
} }
然后进行测试就可以了
总结
总的来说在SpringBoot中使用ActiveMQ已经方便了很多,减少了很多的配置,看起来也更加的清晰了
ActiveMQ的所有配置属性说明
# 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.
最后PS:使用IDEA开发的同学如果看到下面的情况不要强迫症,因为我也很难受
就是红线,但是运行是正常的
参考博客:http://blog.csdn.net/qincidong/article/details/76114434
以ActiveMQ为例JAVA消息中间件学习【3】——SpringBoot中使用ActiveMQ的更多相关文章
- 以ActiveMQ为例JAVA消息中间件学习【2】
前言 之前我们学习了什么是消息中间件,以ActiveMQ为例做了一个最简单的消息中间件的实现.但是我们做的就只能算是个例子而已,因为在实际的项目中肯定会有spring插一脚,所以spring肯定有来管 ...
- 以ActiveMQ为例JAVA消息中间件学习【1】
前言 在慢慢的接触大型的javaweb的项目就会接触到很多的中间件系统. 其中消息中间件在很多场景下会被运用. 这里主要就对最近所学习到的消息中间件知识做一个笔记,为以后的实际运用打下一个良好的基础. ...
- 以ActiveMQ为例JAVA消息中间件学习【4】——消息中间件实际应用场景
前言 当前真正学习消息中间件,当前已经走到了,可以简单的使用,网上有很多那种复杂的高可用的架构,但是那些都是对于一些比较大型的项目来说的. 对于一些小型的项目可能用不到那么大的架构,于是我们需要从最简 ...
- 【Java】学习路径33-在IDEA中使用junit单元测试运行单个方法
首先在菜单找到 文件-项目结构 项目设置-库-"+" 选择Java 找到IDEA安装目录,打开lib/junit4.jar 的jar包,然后打开即可. 然后选择确定. 然后再项目中 ...
- 【java】学习路径18-Arrays中的sort、binarySearch使用注意
在使用Arrays.binarySearch()的时候要注意先对数组进行排序. Arrays.binarySearch()方法介绍: Searches the specified array of i ...
- Java消息中间件入门笔记 - ActiveMQ篇
入门 消息中间件带来的好处: 1)解耦:系统解耦 2)异步:异步执行 3)横向扩展 4)安全可靠 5)顺序保证 栗子: 通过服务调用让其它系统感知事件发生 系统之间高耦合 程序执行效率低 通过消息中间 ...
- Spring Boot学习笔记——Spring Boot与ActiveMQ的集成
Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.这里以ActiveMQ为 ...
- Android(java)学习笔记71:生产者和消费者之等待唤醒机制
1. 首先我们根据梳理我们之前Android(java)学习笔记70中关于生产者和消费者程序思路: 2. 下面我们就要重点介绍这个等待唤醒机制: (1)第一步:还是先通过代码体现出等待唤醒机制 pac ...
- Android(java)学习笔记179:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)
之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播: 1. 我们首先了解一下有序广播和无序广播区别和联系? (1) 有序广播> 接受者 ...
随机推荐
- Python开发——【循环】语句
while循环 while 条件: # 要执行的循环体 # 如果条件为真,那么循环体则执行 # 如果条件为假,那么循环体不执行 死循环 count = 0 while True:# 条件永远为真 pr ...
- Python开发——数据类型【列表】
列表的定义 中括号[]内以逗号分隔开,按照索引,存放各种数据类型,每个位置代表一个元素 list_t = ['张三','Lucy',123] print(list_t) # ['张三', 'Lucy' ...
- 别人的Linux私房菜(5)首次CentOS7与帮助等
ctrl alt F1-F6切换终端tty1-6,其中,F1的终端带有用户界面. 在终端登录后,输入startx启动个人图形界面.(启动有一些条件限制,如没有其他的X Window启用,已经安装,并具 ...
- 20175316盛茂淞 迭代和JDB
迭代和JDB 题目 1 使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 2 m,n 要通过命令行传入 3 提交测试运行截图(至少三张:正常如c( ...
- 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B
牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...
- oracle表空间扩容方法
1.使用navicat连接要扩容的数据库,进入其他-表空间 2.添加数据文件和设置配置项即可
- ABP框架系列之十五:(Caching-缓存)
Introduction ASP.NET Boilerplate provides an abstraction for caching. It internally uses this cache ...
- 子div撑不开父div
方法一:推荐 设置父div的overflow:hidden; 方法二: 父div结束前增加一个空div style=”clear:both;” .clear { clear:both; } <d ...
- Python之路(一)-python简介
一.python简介,python2.x与python3.x的区别 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. Py ...
- HDU 2639 01背包(分解)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 01背包第k优解,把每次的max分步列出来即可 #include<stdio.h> #incl ...
