Spring Boot 整合 ActiveMQ
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--消息队列连接池-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.0</version>
</dependency>
配置文件(application.yml)
server:
port: 61616
spring:
activemq:
broker-url: tcp://localhost:61616
user: admin
password: admin
jms:
pub-sub-domain: false # false=queue true=topic
#定义队列名称
myqueue: activemq-queue
mytopic: activemq-topic
#true 表示使用内置的MQ,false则连接服务器
spring.activemq.in-memory=false
#true表示使用连接池;false时,每发送一条数据创建一个连接
spring.activemq.pool.enabled=true
#连接池最大连接数
spring.activemq.pool.max-connections=10
#空闲的连接过期时间,默认为30秒
spring.activemq.pool.idle-timeout=30000
#强制的连接过期时间,与idleTimeout的区别在于:
idleTimeout是在连接空闲一段时间失效,而expiryTimeout不管当前连接的情况,只要达到指定时间就失效。默认为0,never
spring.activemq.pool.expiry-timeout=0
定义Queue与Topic
@Configuration
@EnableJms
public class QueueBeanConfig {
@Value("${myqueue}")
private String myqueue;
//定义存放消息的队列
@Bean
public Queue queue() {
return new ActiveMQQueue(myqueue);
}
}
@Configuration
@EnableJms
public class TopicBeanConfig {
@Value("${mytopic}")
private String mytopic;
//定义存放消息的队列
@Bean
public Topic topic() {
return new ActiveMQTopic(mytopci);
}
}
生产者
public class QueueProducer {
//注入存放消息的队列,用于下列方法一
@Autowired
private Queue queue;
//注入springboot封装的工具类
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
public void send(String name) {
//方法一:添加消息到消息队列
jmsMessagingTemplate.convertAndSend(queue, name);
//方法二:这种方式不需要手动创建queue,系统会自行创建名为test的队列
jmsMessagingTemplate.convertAndSend("test", name);
}
//间隔时间3s定投,需要在主启动类添加注解:@EnableScheduling
@Scheduled(fixedDelay = 3000)
public void send1(String name) {
jmsMessagingTemplate.convertAndSend(queue, name);
}
}
public class TopicProducer {
//注入存放消息的队列,用于下列方法一
@Autowired
private Topic topic;
//注入springboot封装的工具类
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
public void send(String name) {
jmsMessagingTemplate.convertAndSend(topic, name);
}
//间隔时间3s定投,需要在主启动类添加注解:@EnableScheduling
@Scheduled(fixedDelay = 3000)
public void send1(String name) {
jmsMessagingTemplate.convertAndSend(topic, name);
}
}
消费者
public class QueueConsumer {
// 使用JmsListener配置消费者监听的队列
// @JmsListener如果不指定独立的containerFactory的话只能支持一直模式:或者是点对点,或者是消息订阅
@JmsListener(destination = "${myqueue}")
public void receive(TextMessage textMessage) throws JMSException {
System.out.println(textMessage.getText());
}
// 双向列队:将return回的值,再发送的"out.queue"队列中(其中name是接收到的消息)
// SendTo 会将此方法返回的数据, 写入到 OutQueue 中去.
@JmsListener(destination = "${myqueue}")
@SendTo("outqueue")
public String handleMessage(String name) {
return "成功接受Name:" + name;
}
}
public class TopicCustomer {
/**
* 创建2个消费者
*/
@JmsListener(destination = "mytopic")
public void subscriber(String text) {
System.out.println("消费者1:" + text);
}
@JmsListener(destination = "mytopic")
public void subscriber1(String text) {
System.out.println("消费者2:" + text);
}
}
Spring Boot 整合 ActiveMQ的更多相关文章
- spring boot整合activemq消息中间件
spring boot整合activemq消息中间件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...
- activeMQ入门+spring boot整合activeMQ
最近想要学习MOM(消息中间件:Message Oriented Middleware),就从比较基础的activeMQ学起,rabbitMQ.zeroMQ.rocketMQ.Kafka等后续再去学习 ...
- spring boot 整合activemq
1 Spring Boot与ActiveMQ整合 1.1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <properties> <spring.version& ...
- Spring Boot入门 and Spring Boot与ActiveMQ整合
1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...
- Spring Boot与ActiveMQ整合
Spring Boot与ActiveMQ整合 1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <dependency> <groupId>org.spri ...
- RabbitMQ使用及与spring boot整合
1.MQ 消息队列(Message Queue,简称MQ)——应用程序和应用程序之间的通信方法 应用:不同进程Process/线程Thread之间通信 比较流行的中间件: ActiveMQ Rabbi ...
- Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 预见未来最好的方式就是亲手创造未来 – <史蒂夫·乔布斯传> 』 运行环境: ...
- spring boot整合jsp的那些坑(spring boot 学习笔记之三)
Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency> <groupId>or ...
- spring boot 系列之四:spring boot 整合JPA
上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...
随机推荐
- jsp-提交表单乱码解决
jsp提交表单有两种方式,一种是get,一种是post,对于两种方式都可能出现乱码,以下给出两种乱码方式的解决方案. 1.post提交解决乱码 //设置解码方式,post提交解决乱码 比较简单 req ...
- 剑指offer——25合并两个排序的链表
题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. 题解: 使用普通方法,或者递归,注意新的头节点即可. //使用普通的合并方法 class S ...
- Android Telephony分析(一) ---- Phone详解
目录: Phone的继承关系与PhoneFactory(GsmCdmaPhone.ImsPhone.SipPhone) Phone进程的启动 Phone对象的初始化(DefaultPhoneNotif ...
- Mysql 插入数据,随机事件选择
在拼写sql的 时候,mysql字段如果需要添加当前时间可以用NOW() 函数 // String sql = ("insert into tablename(content, create ...
- 天道神诀---防火墙以及selinux(上篇)
Linux防火墙 linux6.x 防火墙会影响通信,默认是拒绝所有. [root@redhat6 sysconfig]# chkconfig iptables --listiptables ...
- 15-Ubuntu-文件和目录命令-查看目录内容-ls-2
4. ls和通配符的使用 通配符适用的地方:shell命令行或者shell脚本中. 正则表达式适用的地方:字符串处理时,一般有一般正则和Perl正则. 正则表达式与通配符有相同的符号但是意义不同!! ...
- 深度探索C++对象模型之第二章:构造函数语意学之Copy constructor的构造操作
C++ Standard将copy constructor分为trivial 和nontrivial两种:只有nontrivial的实例才会被合成于程序之中.决定一个copy constructor是 ...
- axios调用接口
axios调用接口 1. 按照axiosnpm install --save-dev axios2.在main.js 引入axios, 设置全局属性$http 指向axios main.js impo ...
- .Net 动态编译(c# 脚本)
1 用.NET提供的类动态编译代码字符串,生成DLL存于内存中,加载到程序域 2 用反射的方式调用这个DLL 将要被编译和执行的代码读入并以字符串方式保存声明CSharpCodeProvider对象实 ...
- POJ 3134 - Power Calculus
迭代加深 //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include<al ...