RocketMQ整合SpringBoot普通消息

复制代码
<!--匹配服务器的RocketMQ5.3.0-->
<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>2.3.1</version>
</dependency>

application.properties

spring.application.name=springboot-rocketmq
server.port=8999



rocketmq.name-server=xxx.xxx.xxx:9876
rocketmq.producer.group=mq_producer_group_test

控制器

package com.example.springbootrocketmq.controller;

import com.example.springbootrocketmq.pojo.User;
import com.example.springbootrocketmq.producer.RocketMQProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author hrui
 * @date 2024/8/2 11:40
 */
@RestController
@RequestMapping("/api/test")
public class TestController {

    @Autowired
    private RocketMQProducerService producerService;

    @GetMapping("/send")
    public String sendMessage() {
        User user = new User("Hrui", 18, "China");
        producerService.sendSimpleMessage("mq_test-topic", user);
        return "消息发送成功";
    }
}

生产者

package com.example.springbootrocketmq.producer;

import com.example.springbootrocketmq.pojo.User;
import jakarta.annotation.Resource;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author hrui
 * @date 2024/8/2 11:36
 */
@Service
public class RocketMQProducerService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    /**
     * 发送普通消息
     * @param topic
     * @param message
     */
    public void sendSimpleMessage(String topic, User message) {
        rocketMQTemplate.convertAndSend(topic, message);
    }
}

消费者

package com.example.springbootrocketmq.consumer;

import com.example.springbootrocketmq.pojo.User;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

/**
 * @author hrui
 * @date 2024/8/2 11:44
 */
@Service
@RocketMQMessageListener(topic = "mq_test-topic", consumerGroup = "mq_consumer_group_test")
public class RocketMQConsumerService implements RocketMQListener<User> {
    @Override
    public void onMessage(User user) {
        System.out.println("消费者接收到消息: " + user);
    }
}
相关推荐
bjzhang7521 分钟前
SpringBoot开发——集成Tess4j实现OCR图像文字识别
spring boot·ocr·tess4j
flying jiang26 分钟前
Spring Boot 入门面试五道题
spring boot
小菜yh27 分钟前
关于Redis
java·数据库·spring boot·redis·spring·缓存
爱上语文2 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
荆州克莱2 小时前
springcloud整合nacos、sentinal、springcloud-gateway,springboot security、oauth2总结
spring boot·spring·spring cloud·css3·技术
serve the people2 小时前
springboot 单独新建一个文件实时写数据,当文件大于100M时按照日期时间做文件名进行归档
java·spring boot·后端
罗政8 小时前
[附源码]超简洁个人博客网站搭建+SpringBoot+Vue前后端分离
vue.js·spring boot·后端
Java小白笔记11 小时前
关于使用Mybatis-Plus 自动填充功能失效问题
spring boot·后端·mybatis
小哇66611 小时前
Spring Boot,在应用程序启动后执行某些 SQL 语句
数据库·spring boot·sql
luoluoal13 小时前
java项目之企业级工位管理系统源码(springboot)
java·开发语言·spring boot