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);
    }
}
相关推荐
C++chaofan1 小时前
基于session实现短信登录
java·spring boot·redis·mybatis·拦截器·session
Filotimo_2 小时前
SpringBoot3入门
java·spring boot·后端
一 乐2 小时前
校园墙|校园社区|基于Java+vue的校园墙小程序系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·小程序
vx_bscxy3222 小时前
告别毕设焦虑!Python 爬虫 + Java 系统 + 数据大屏,含详细开发文档 基于微信小程序的民宿预约系统22398 (上万套实战教程,赠送源码)
java·spring boot·mysql·微信小程序·课程设计
q***06295 小时前
Spring Boot + Spring AI快速体验
人工智能·spring boot·spring
后端小张5 小时前
【JAVA 进阶】Spring Boot 注解体系与工程实践
java·开发语言·spring boot·后端·spring·spring cloud·java-ee
考虑考虑6 小时前
springboot中yml、yaml、properties加载顺序
spring boot·后端·spring
隐形喷火龙7 小时前
Springboot集成OnlyOffice
java·spring boot·后端
5pace7 小时前
【SSM|第一篇】MyBatisPlus
java·spring boot·后端·mybatis
JosieBook7 小时前
【SpringBoot】37 核心功能 - 高级特性- Spring Boot 中的 自定义 Starter 完整教程
java·spring boot·后端