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);
    }
}
相关推荐
小编码上说1 小时前
LSH(局部敏感哈希)分桶,海量数据下的相似性搜索解决方案
java·spring boot·缓存·langchain4j·lsh·局部敏感哈希·ai调用优化
计算机_毕业设计1 小时前
java-springboot数字藏品系统 基于 SpringBoot 的区块链数字艺术品交易平台 Java 微服务架构下的加密藏品展示与拍卖系统计算机毕业设计
java·spring boot·课程设计
dovens2 小时前
SpringBoot集成MQTT客户端
java·spring boot·后端
❀͜͡傀儡师2 小时前
Spring Boot 集成 RocksDB 实战:打造高性能 KV 存储加速层
java·spring boot·后端·rocksdb
NaMM CHIN3 小时前
Spring Boot + Spring AI快速体验
人工智能·spring boot·spring
ATCH IERV3 小时前
Java实战:Spring Boot application.yml配置文件详解
java·网络·spring boot
钰衡大师5 小时前
Activiti 7 工作流技术文档
java·数据库·spring boot
Ruci ALYS6 小时前
SpringBoot Maven快速上手
spring boot·后端·maven
rADu REME6 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
你好潘先生6 小时前
Next.js + Spring Boot 实现 AI 多模型并行对话系统(架构设计与关键实现)
spring boot·向量检索·next.js·pgvector·ai对话·多模型对比·sse流式输出