Java SpringBoot如何整合ActiveMQ
发表于:2025-12-03 作者:千家信息网编辑
千家信息网最后更新 2025年12月03日,Java SpringBoot如何整合ActiveMQ,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、 如果要想在项目
千家信息网最后更新 2025年12月03日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安全错误
数据库的锁怎样保障安全
烽火通信软件开发武汉工资
网络安全审计员职责
华服集团互联网科技
地理数据导入pg数据库
上海魁捷互联网科技有限公司
数据库出版物的定义
mysql创建复制数据库
软件开发公司的销售场景
阿里服务器怎么用
网络技术运维方案
我的世界服务器攻击间隔时间
汉王出现数据库操作错误
计算机网络技术常用英语
承接辅助软件开发
网站服务器 php
广东前端软件开发哪家专业
dns服务器分析出错
运营一般用到的数据库
铜川服务器维保
企业网络安全事件
网易花雨庭服务器的房子
hbase后端图数据库
汇丰软件开发招聘
兼职什么软件开发
数据库及web技术
小型服务器租用
迎进博网络安全
c 更新数据库表的代码
mc网易版抵御僵尸的服务器在哪
软件开发简历包装进了公司