以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) 有序广播> 接受者 ...
随机推荐
- linux下安装python,Django,虚拟环境
linux下python安装步骤: 1.下载python3源码包 wget https://www.python.org/ftp/python/3.6.6/Python-3.6.7.tgz 2.下载p ...
- Python开发——数据类型【集合】
集合的定义 由一个或多个确定的元素所构成的整体 可变集合 s=set('hello') print(s) # {'e', 'l', 'o', 'h'} s=set(['alex','alex','Lu ...
- 九校联考_24OI——餐馆restaurant
凉心模拟D1T1--最简单的一道题 TAT 餐馆(restaurant) 题目背景 铜企鹅是企鹅餐馆的老板,他正在计划如何使得自己本年度收益增加. 题目描述 共有n 种食材,一份食材i 需要花ti 小 ...
- linux学习第七天 (Linux就该这么学)
今天讲了chmod (权限 设置)和 chown(属性 设置),特殊权限:SUID u+s 数字法是4 x=s - = S,SGID g+s 数字法是2 x=s -=S,SBIT o+t x=t ...
- idea环境配置
Idea 下载 https://www.jetbrains.com/idea/download/#section=windows idea安装(略) idea破解 window配置hosts文件:0. ...
- django by example 第五章 No module named 'sorl-thumbnail'
描述:按照原书在settings的installed apps中加入sorl-thumbnail后同步数据库显示No module named 'sorl-thumbnail' 解决方案: 根据官方文 ...
- 第一次spring会议
1.今天查询了很多案例,找到了符合我们要求的案例,并进行了尝试. 2.昨天拍摄了宣传视频. 3.明天准备用C#限定格式输出TXT文件.
- python 安装第三方包时 read timed out
记录下安装python第三方包超时报错,解决方法:(以安装numpy为例) pip install numpy 报错:raise ReadTimeoutError(self._pool, None, ...
- 【Mybatis】MyBatis快速入门(一)
Mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...
- 2019.02.11 bzoj4818: [Sdoi2017]序列计数(矩阵快速幂优化dp)
传送门 题意简述:问有多少长度为n的序列,序列中的数都是不超过m的正整数,而且这n个数的和是p的倍数,且其中至少有一个数是质数,答案对201704082017040820170408取模(n≤1e9, ...
