-
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
相关推荐
考虑考虑8 小时前
Jpa使用union all阿杆18 小时前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator昵称为空C1 天前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用阿里云云原生2 天前
海量接入、毫秒响应:易易互联携手阿里云构筑高可用物联网消息中枢麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群麦兜*2 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案汤姆yu2 天前
基于springboot的毕业旅游一站式定制系统计算机毕业设计木哥2 天前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】gtGsl_2 天前
深入解析 Apache RocketMQ架构组成与核心组件作用hdsoft_huge2 天前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范