-
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
相关推荐
原来是好奇心21 小时前
消息队列终极选型:RabbitMQ、RocketMQ、Kafka与ActiveMQ深度对比q***563821 小时前
Spring Boot--@PathVariable、@RequestParam、@RequestBodyFREE技术21 小时前
学生成绩管理系统 基于java+springboot+vue实现前后端分离项目并附带万字文档(源码+数据库+万字详设文档+软件包+安装教程)q***57501 天前
Spring Boot(七):Swagger 接口文档百***07181 天前
IDEA+Docker一键部署项目SpringBoot项目q***51891 天前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析期待のcode1 天前
Springboot主配置文件雾林小妖1 天前
springboot实现跨服务调用/springboot调用另一台机器上的服务Code88481 天前
观察Springboot AI-Function Tools 执行过程聆风吟º1 天前
【Spring Boot 报错已解决】Spring Boot开发避坑指南:Hibernate实体类主键配置详解与异常修复