-
pom.xml导入RocketMQ依赖
<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> -
application.yml中添加配置
rocketmq: name-server: 127.0.0.1:9876 producer: group: x access-key: myaccesskey secret-key: mysecretKey send-message-timeout: 10000 tls-enable: true consumer: group: x access-key: myaccesskey secret-key: mysecretKey tls-enable: true -
创建MQ工具类
public class MqUtil { private final RocketMQTemplate rocketMQTemplate; public MqUtil(RocketMQTemplate rocketMQTemplate) { this.rocketMQTemplate = rocketMQTemplate; } /** * 单条通知发送 * * @param topic 主题 * @param message 消息 */ public void convertAndSend(String topic, Object message) { rocketMQTemplate.convertAndSend(topic, message); } /** * 批量通知发送 * * @param topic 主题 * @param messages 消息集合 */ public <T extends Message<?>> SendResult syncSend(String topic, Collection<T> messages) { return rocketMQTemplate.syncSend(topic, messages); } /** * 批量通知发送 * * @param topic 主题 * @param messages 消息集合 * @param sendCallback 回调函数 */ public <T extends Message<?>> void asyncSend(String topic, Collection<T> messages, SendCallback sendCallback) { rocketMQTemplate.asyncSend(topic, messages, sendCallback); } } -
注入工具类Bean
@Bean public MqUtil mqUtil(RocketMQTemplate rocketMQTemplate) { return new MqUtil(rocketMQTemplate); } -
测试发消息
@Resource private MqUtil mqUtil; @Test public void test() { mqUtil.convertAndSend(TopicConstant.TOPIC_B, "123456"); } -
订阅接收消息
@Slf4j @Service @RequiredArgsConstructor(onConstructor = @__({@Autowired})) @RocketMQMessageListener(consumerGroup = GroupConstant.GROUP_A, topic = TopicConstant.TOPIC_B) public class TopicAConsumer implements RocketMQListener<Message> { @Override public void onMessage(Message message) { log.info(JsonUtil.toJsonStr(message)); } }
Spring Boot整合RocketMQ
阿拉狗2023-12-28 8:02
相关推荐
李慕婉学姐22 分钟前
【开题答辩过程】以《基于Spring Boot的疗养院理疗管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看qq_124987075331 分钟前
基于协同过滤算法的运动场馆服务平台设计与实现(源码+论文+部署+安装)一路向北35 分钟前
Spring Boot 3 整合 SSE (Server-Sent Events) 企业级最佳实践(一)风象南41 分钟前
JFR:Spring Boot 应用的性能诊断利器爱吃山竹的大肚肚1 小时前
微服务间通过Feign传输文件,处理MultipartFile类型qq_12498707531 小时前
基于springboot的尿毒症健康管理系统的设计与实现(源码+论文+部署+安装)编程彩机1 小时前
互联网大厂Java面试:从Spring Boot到微服务优化场景解析猿小羽2 小时前
Flyway + Spring Boot:实现数据库迁移的最佳实践毕设源码-邱学长3 小时前
【开题答辩全过程】以 基于Springboot的酒店住宿信息管理系统的设计与实现为例,包含答辩的问题和答案gAlAxy...4 小时前
SpringBoot Servlet 容器全解析:嵌入式配置与外置容器部署