SpringBoot 2.x (13):整合ActiveMQ
ActiveMQ5.x不多做介绍了,主要是SpringBoot的整合
特点:
1)支持来自Java,C,C ++,C#,Ruby,Perl,Python,PHP的各种跨语言客户端和协议
2)支持许多高级功能,如消息组,虚拟目标,通配符和复合目标
3) 完全支持JMS 1.1和J2EE 1.4,支持瞬态,持久,事务和XA消息
4) Spring支持,ActiveMQ可以轻松嵌入到Spring应用程序中,并使用Spring的XML配置机制进行配置
5) 支持在流行的J2EE服务器(如TomEE,Geronimo,JBoss,GlassFish和WebLogic)中进行测试
6) 使用JDBC和高性能日志支持非常快速的持久化
下载:
http://activemq.apache.org/activemq-5153-release.html
实际开发推荐部署到Linux系统,具体操作网上也有教程
我这里为了方便,直接安装在本地Windows机器上
如果想了解更多,查看官方文档:
http://activemq.apache.org/getting-started.html
进入bin目录win64目录启动activemq.bat即可
访问localhost:8161进入首页
访问http://localhost:8161/admin/进入管理页面,默认用户名和密码都是admin
整合:
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
连接池
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
基本的配置
# ActiveMQ
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100
使用ActiveMQ必须要在SpringBoot启动类中开启JMS,并进行配置
package org.dreamtech.avtivemq; import javax.jms.ConnectionFactory; import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate; @SpringBootApplication
@EnableJms
public class AvtivemqApplication { public static void main(String[] args) {
SpringApplication.run(AvtivemqApplication.class, args);
} @Autowired
private Environment env; @Bean
public ConnectionFactory connectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(env.getProperty("spring.activemq.broker-url"));
connectionFactory.setUserName(env.getProperty("spring.activemq.user"));
connectionFactory.setPassword(env.getProperty("spring.activemq.password"));
return connectionFactory;
} @Bean
public JmsTemplate genJmsTemplate() {
return new JmsTemplate(connectionFactory()); } @Bean
public JmsMessagingTemplate jmsMessageTemplate() {
return new JmsMessagingTemplate(connectionFactory());
}
}
点对点模型:
首先实现消息的发送
package org.dreamtech.avtivemq.service; import javax.jms.Destination; /**
* 消息生产
*
* @author Xu Yiqing
*
*/
public interface ProducerService {
/**
* 使用指定消息队列发送
*
* @param destination
* @param message
*/
void sendMsg(Destination destination, final String message);
}
package org.dreamtech.avtivemq.service.impl; import javax.jms.Destination; import org.dreamtech.avtivemq.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service; @Service
public class ProducerServiceImpl implements ProducerService {
@Autowired
private JmsMessagingTemplate jmsTemplate; @Override
public void sendMsg(Destination destination, String message) {
jmsTemplate.convertAndSend(destination, message);
} }
package org.dreamtech.avtivemq.controller; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue;
import org.dreamtech.avtivemq.service.ProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class OrderController {
@Autowired
private ProducerService producerService; @GetMapping("/order")
private Object order(String msg) {
Destination destination = new ActiveMQQueue("order.queue");
producerService.sendMsg(destination,msg);
return "order";
}
}
访问:http://localhost:8080/order?msg=demo,然后查看ActiveMQ界面:

有生产者就就有消费者:监听消息队列
package org.dreamtech.avtivemq.jms; import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; @Component
public class OrderConsumer {
/**
* 监听指定消息队列
*
* @param text
*/
@JmsListener(destination = "order.queue")
public void receiveQueue(String text) {
System.out.println("[ OrderConsumer收到的报文 : " + text + " ]");
}
}
由于实时监听,一启动SpringBoot就会打印:
[ OrderConsumer收到的报文 : demo ]
发布订阅模型:比如抖音小视频,某网红发布新视频,多名粉丝收到消息
默认ActiveMQ只支持点对点模型,想要开启发布订阅模型,需要进行配置
spring.jms.pub-sub-domain=true
Spring管理主题对象
@Bean
public Topic topic() {
return new ActiveMQTopic("demo.topic");
}
发布者
/**
* 消息发布者
*
* @param msg
*/
void publish(String msg);
@Autowired
private JmsMessagingTemplate jmsTemplate;
@Autowired
private Topic topic; @Override
public void publish(String msg) {
jmsTemplate.convertAndSend(topic, msg);
}
@Autowired
private ProducerService producerService;
@GetMapping("/topic")
private Object topic(String msg) {
producerService.publish(msg);
return "success";
}
订阅者(消费者):一人发布,多人订阅
package org.dreamtech.avtivemq.jms; import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; @Component
public class TopicConsumer {
@JmsListener(destination = "demo.topic")
public void receiver1(String text) {
System.out.println("TopicConsumer : receiver1 : " + text);
} @JmsListener(destination = "demo.topic")
public void receiver2(String text) {
System.out.println("TopicConsumer : receiver2 : " + text);
} @JmsListener(destination = "demo.topic")
public void receiver3(String text) {
System.out.println("TopicConsumer : receiver3 : " + text);
}
}
启动项目,访问:
http://localhost:8080/topic?msg=666
打印如下
TopicConsumer : receiver1 : 666
TopicConsumer : receiver3 : 666
TopicConsumer : receiver2 : 666
那么点对点和发布订阅模型可以一起使用吗?
不可以
如何配置?
1.注释掉 #spring.jms.pub-sub-domain=true
2.加入Bean:给topic定义独立的JmsListenerContainer
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(activeMQConnectionFactory);
return bean;
}
3.@JmsListener如果不指定独立的containerFactory的话是只能消费queue消息
@JmsListener(destination = "demo.topic", containerFactory = "jmsListenerContainerTopic")
public void receiver1(String text) {
System.out.println("TopicConsumer : receiver1 : " + text);
}
SpringBoot 2.x (13):整合ActiveMQ的更多相关文章
- SpringBoot整合ActiveMQ和开启持久化
一.点对点 1.提供者目录展示 2.导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...
- Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ
集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...
- springboot整合ActiveMQ,配置问题
1.ActiveMQ的安装和相关配置修改 去官网下载安装包解压至文件夹 双击打开 打开浏览器输入 http://127.0.0.1:8161 到此activeMQ就安装好了 2.springboot工 ...
- 【springboot】之整合ActiveMQ
1.引入依赖的jar <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...
- SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...
- SpringBoot整合ActiveMQ快速入门
Spring Boot 具有如下特性: 为基于 Spring 的开发提供更快的入门体验 开箱即用,没有代码生成,也无需 XML 配置.同时也可以修改默认值来满足特定的需求. 提供了一些大型项目中常见的 ...
- SpringBoot第二十一篇:整合ActiveMQ
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11190048.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 前一章节中 ...
- 解决Springboot整合ActiveMQ发送和接收topic消息的问题
环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...
- ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...
- springboot整合activemq加入会签,自动重发机制,持久化
消费者客户端成功接收一条消息的标志是:这条消息被签收. 消费者客户端成功接收一条消息一般包括三个阶段: 1.消费者接收消息,也即从MessageConsumer的receive方法返 ...
随机推荐
- Oracle创建表,并添加默认值和备注
create table testemp( id varchar2(50) default sys_guid(),deptno varchar2(20) ,--部门编码 ename varchar2( ...
- cassandra根据用户名密码登录cqlsh
修改conf目录下cassandra.yaml文件 authenticator: PasswordAuthenticator //将authenticator修改为PasswordAuthentic ...
- warning: conflicting types for built-in function 'puts'
warning: conflicting types for built-in function 'puts' [编译器版本] arm-linux-gcc 3.4.1 [问题描述] 在做嵌入式底层开发 ...
- iterator与iterable的区别和联系
iterator与iterable 用Iterator模式实现遍历集合Iterator模式是用于遍历集合类的标准访问方法.它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内 ...
- complexType
//decltype的表达式如果是加上括号的变量,结果将是引用 decltype((variable)) ruiy; //此变量的数据类型是引用(但此处变量的申明语句是错误的,引用不是对象,指向的对象 ...
- 序列联配(alignment)和数据库搜索方法简介
根据一个打分系统,怎么样排对起来打分能够最大.就认为历史上应该是这样子的. 数据同源搜索软件Fasta和Blast 是目前功能最全,使用最广的同源性数据库搜索软件包.他们在Needleman的动态算法 ...
- The web.config file for this project is missing the required DirectRequestModule.
The web.config file for this project is missing the required DirectRequestModule. 将应用程序集的模式由集成改为经典 ...
- Linux 错误集锦
1. CentOS 7 运行yum时出现/var/run/yum.pid已被锁定,PID为xxxx的另一个程序正在运行的问题解决 解决办法: rm -f /var/run/yum.pid,删除文件后再 ...
- SqlHelper 增删改查
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- CF1119F Niyaz and Small Degrees【treedp+堆】
如果枚举d来dp,那么就是设f[u][0/1]为u点不断/断掉和父亲的边,然后优先选取f[v][1]+w(u,v)<=f[v][0]的,如果断掉这些度数还是多就用一个堆维护剩下的按f[v][1] ...