Springboot中怎么对ActiveMQ进行整合
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,Springboot中怎么对ActiveMQ进行整合,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1、首先新建一个spr
千家信息网最后更新 2025年12月02日Springboot中怎么对ActiveMQ进行整合
Springboot中怎么对ActiveMQ进行整合,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
1、首先新建一个springboot工程(新建过程略),本文springboot版本 是2.1.1.RELEASE
2、在pom.xml文件添加activemq的相关依赖
org.springframework.boot spring-boot-starter-activemq org.apache.activemq activemq-pool org.messaginghub pooled-jms
其中连接池相关的依赖可以不用配置
3、配置application.properties 或者application.yml 本文以application.properties为例
spring.activemq.broker-url=tcp://localhost:61616spring.activemq.user=adminspring.activemq.password=admin#默认情况下activemq提供的是queue模式,若要使用topic模式需要配置下面配置#spring.jms.pub-sub-domain=true#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=15000
spring.activemq.pool.enabled=true时要在pom文件中添加连接池pool相关的依赖,为false时不用添加连接池pool相关的依赖;
若使用连接池pool配置时,注意两种依赖的配置否则启动失败。
工程结构如下图
Demo代码如下
package com.example.acmpp.config;import javax.jms.Queue;import javax.jms.Topic;import org.apache.activemq.command.ActiveMQQueue;import org.apache.activemq.command.ActiveMQTopic;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class BeanConfig { //定义存放消息的队列 @Bean public Queue queue() { return new ActiveMQQueue("ActiveMQQueue"); } //定义存放消息的队列 @Bean public Topic topic() { return new ActiveMQTopic("ActiveMQTopic"); }}消息生产者代码
import javax.jms.Queue;import javax.jms.Topic;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/* * * 消息生产者 */@RestControllerpublic class ProviderController { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Autowired private Topic topic; /** * 消息生产者 Queue模式 * */ @RequestMapping("/sendQ") public void sendQ(String msg) { //方法一:添加消息到消息队列 jmsMessagingTemplate.convertAndSend(queue, msg); //方法二:这种方式不需要手动创建queue,系统会自行创建名为test的队列 //jmsMessagingTemplate.convertAndSend("testQ", msg); } /** * 消息生产者 Topic模式 * @param msg */ @RequestMapping("/sendT") public void sendT(String msg) { // 指定消息发送的目的地及内容 System.out.println("@@@@@@@@@@@@@@" + msg); this.jmsMessagingTemplate.convertAndSend(this.topic, msg); }}消息消费者代码
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.annotation.JmsListener;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.messaging.handler.annotation.SendTo;import org.springframework.stereotype.Component;/** * 消息消费者 * @author FFF * */@Componentpublic class ConsumerService { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; /** * 消费ActiveMQQueue */ // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息 @JmsListener(destination = "ActiveMQQueue") // SendTo 会将此方法返回的数据, 写入到 OutQueue 中去. @SendTo("SQueue") public String handleMessage(String name) { System.out.println("ActiveMQQueue成功接受Name" + name); return "ActiveMQQueue成功接受Name" + name; } /** * 消费ActiveMQ.DLQ */ // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息 @JmsListener(destination = "ActiveMQ.DLQ") public void DLQ(String name) { System.out.println("ActiveMQ.DLQ成功接受Name==" + name); } /** * 消费SQueue */ // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息 @JmsListener(destination = "SQueue") public void SQueue(String name) { System.out.println("SQueue成功接受Name==" + name); } /** * 消费testQ */ // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息 @JmsListener(destination = "testQ") public void testQMessage(String name) { System.out.println("testQ成功接受Name" + name); } /** * 消费topic * */ // 使用JmsListener配置消费者监听的队列,其中name是接收到的消息 @JmsListener(destination = "ActiveMQTopic") public void topicMessage(String name) { System.out.println("topicMessage成功接受Name" + name); }}看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
消息
消费
配置
队列
消费者
成功
监听
模式
生产者
生产
代码
不用
内容
工程
数据
文件
方法
帮助
整合
最大
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
监理建设工程进度网络技术
2000怎么导出数据库备份
腾讯云2核4g服务器老是断线
PLC数据库代码
在网络技术有限公司做客服怎么样
淮北电商平台软件开发公司
linux服务器限制平板访问
日照企业党建软件开发电话
宏顺源网络技术中心
吃鸡如何使用网络技术
网络技术人员职位描述
clds数据库
镇江百事通网络技术有限公司
c 读取本地数据库文件
静海区数据网络技术诚信合作
网络安全技术 讲座
我的世界外部服务器地址
ups供电服务器实现自动关机
服务器网卡不可用怎么办
网络安全在国内发展
微课资源数据库
关于网络安全威胁发展趋势
招商银行app服务器异常
数据库图形的建模
武汉市千应网络技术有限公司
如何清理e4a编译数据库
乡镇网络安全考核管理办法
上海甚术网络技术怎么样
pcl2无法连接mod服务器
数据库学校专业字段