Java SpringBoot如何整合ActiveMQ
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,Java SpringBoot如何整合ActiveMQ,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、 如果要想在项目
千家信息网最后更新 2026年11月01日Java SpringBoot如何整合ActiveMQ
Java SpringBoot如何整合ActiveMQ,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
一、 如果要想在项目之中去使用 ActiveMQ 组件,则应该为项目添加依赖支持库,修改 pom.xml 配置文件:
org.springframework.boot spring-boot-starter-activemq
二、修改 application.yml 配置文件进行 activemq 的配置;
server: port: 80spring: messages: basename: i18n/Messages,i18n/Pages jms: pub-sub-domain: false # 配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息 activemq: user: studyjava # 连接用户名 password: hello # 连接密码 broker-url: tcp://activemq-server:61616 # 消息组件的连接主机信息
三、 随后定义一个消息的消费者,消费者主要是进行一个监听控制,在 SpringBoot 里面可以直接利用注解@JmsListener进行监听:
package cn.study.microboot.consumer;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Service;@Servicepublic class MessageConsumerService { @JmsListener(destination="study.msg.queue") public void receiveMessage(String text) { // 进行消息接收处理 System.err.println("【*** 接收消息 ***】" + text); }}四、 随后建立消息的发送者服务,一般而言如果进行消息的发送往往会准备出一个业务接口来:
package cn.study.microboot.producer;public interface IMessageProducerService { public void sendMessage(String msg) ; }五、随后建立一个配置程序类,定义 ActiveMQ 的消息发送模版处理类:
package cn.study.microboot.config;import javax.jms.Queue;import org.apache.activemq.command.ActiveMQQueue;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jms.annotation.EnableJms;@Configuration@EnableJmspublic class ActiveMQConfig { @Bean public Queue queue() { return new ActiveMQQueue("study.msg.queue") ; }}六、创建消息发送的子类实现消息发送处理:
package cn.study.microboot.producer.impl;import javax.annotation.Resource;import javax.jms.Queue;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.stereotype.Service;import cn.study.microboot.producer.IMessageProducerService;@Servicepublic class MessageProducerServiceImpl implements IMessageProducerService { @Resource private JmsMessagingTemplate jmsMessagingTemplate; @Resource private Queue queue; @Override public void sendMessage(String msg) { this.jmsMessagingTemplate.convertAndSend(this.queue, msg); }}七、编写测试类来观察消息的处理:
package cn.study.microboot.test;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import cn.study.microboot.StartSpringBootMain;import cn.study.microboot.producer.IMessageProducerService;@SpringBootTest(classes = StartSpringBootMain.class)@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfigurationpublic class TestActiveMQ { @Resource private IMessageProducerService messageProducer; @Test public void testSend() throws Exception { for (int x = 0; x < 10; x++) { this.messageProducer.sendMessage("study - " + x); } }}看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
消息
配置
处理
文件
消费者
组件
项目
帮助
支持
消费
监听
整合
清楚
一般而言
业务
主机
之中
信息
内容
发送者
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
河南云网互联网络科技有限公司
中国 科技 互联网
成都互联网科技公司
贷财互联网科技
宜安科技 互联网金融
广州互联网络科技有限公司
伴你行互联网科技有限公司
互联网和高科技公司
成都铂石互联网科技做什么的
互联网与医疗科技
互联网科技人工智能实验室
威龙国际互联网科技公司
恒生互联网科技业指数调整周期
银行科技岗属于互联网吗
杭州披云加互联网科技有限公司
工业互联网科技成果转化
下一代互联网重大应用科技园
35岁以上互联网科技行业
上海同邻互联网科技有限公司规模
黑龙江互联网科技公司
互联网黑科技
互联网科技图片
合肥互联网科技公司
科技大学互联网专业好不好
互联网科技和信息
互联网科技的全部知识
互联网科技企业商标注意事项
科技和互联网行业的特点
马化腾互联网科技教学
互联网科技和医药行业