Springboot中怎么对ActiveMQ进行整合
发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,Springboot中怎么对ActiveMQ进行整合,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。1、首先新建一个spr
千家信息网最后更新 2025年02月07日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安全错误
数据库的锁怎样保障安全
服务器上的硬盘怎么拷贝到电脑上
大学网络安全视频
安全管理平台服务器设置
网络安全的基本要素主要包括
推广网络安全法建议
库存软件连接数据库失败
无法连接服务器超时是什么意思
头条小程序提供数据库吗
sql删除表内前三行数据库
云南省省检察院网络安全工作
软件开发工作中的不足
多特蒙德数据库
网络安全和软件测试那个简单
小程序源码带数据库
nginx作为前端项目服务器
苹果贵州服务器云主机
大学生网络安全教育的对策
青岛联想服务器代理在哪里
瑞庭网络技术汽车金融招聘
金山区特殊软件开发排行
材料网络安全法材料一材料二
如何用代理服务器装谷歌地球
查看当前dns服务器
阿里云服务器安全机制
计算机网络技术第二版第9章
网络安全助成长
枣庄学院软件开发
r710服务器怎么设置自动开机
刘强东事件网络安全
英灵神殿不显示好友服务器