springboot 搭建一个 测试rabbitmq连通性demo

1.pom

yaml 复制代码
		<!-- Spring Boot Starter for RabbitMQ -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>

2.config

java 复制代码
package com.example.demo.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author wangjn
 * @Description
 */
@Configuration
public class RabbitMQConfig {

    @Bean
    public Queue myQueue() {
        // 这里可以根据需要配置队列的属性,例如:
        // 是否持久化、是否排他、是否自动删除、队列参数等
        return new Queue("myQueue", true); // 第二个参数true表示队列持久化
    }
}

3.service

java 复制代码
package com.example.demo.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

/**
 * @author wangjn
 * @Description
 * @createTime 2024-06-13 14:03:00
 */
@Service
public class RabbitMqService {

    private final RabbitTemplate rabbitTemplate;

    public RabbitMqService(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMessage(String queueName, String message) {
        rabbitTemplate.convertAndSend(queueName, message);
    }
}

4.controller

java 复制代码
package com.example.demo.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wangjn
 * @Description
 * @createTime 2024-06-13 14:09:00
 */
@RestController
public class RabbitMQController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RabbitMQController.class);
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("/sendToRabbit")
    public String sendMessageToRabbit(@RequestParam("message") String message) {
        rabbitTemplate.convertAndSend("myQueue", message);
        return "Message sent to RabbitMQ successfully!";

    }

    @GetMapping("/checkRabbitMessages")
    public String checkForMessages() {
        return "Checking for messages...";
    }

    @RabbitListener(queues = "myQueue")
    public void listenToRabbit(String message) {
        LOGGER.info("Message received from RabbitMQ: {}", message);
    }

}
相关推荐
sg_knight2 小时前
Spring 框架中的 SseEmitter 使用详解
java·spring boot·后端·spring·spring cloud·sse·sseemitter
Dolphin_Home4 小时前
笔记:SpringBoot静态类调用Bean的2种方案(小白友好版)
java·spring boot·笔记
刘一说5 小时前
Nacos 权限控制详解:从开源版 v2.2+ 到企业级安全实践
spring boot·安全·spring cloud·微服务·nacos·架构·开源
Q_Q5110082856 小时前
python+django/flask+vue的大健康养老公寓管理系统
spring boot·python·django·flask·node.js
czlczl200209256 小时前
通过哪些条件确定用哪个消息转换器
spring boot
毕设源码-朱学姐7 小时前
【开题答辩全过程】以 个人健康管理系统为例,包含答辩的问题和答案
java·spring boot
qq_12498707537 小时前
基于微信小程序的线下点餐系统的设计与实现(源码+论文+部署+安装)
spring boot·微信小程序·小程序·毕业设计
IT_Octopus7 小时前
Java GZip 压缩实践 +实践思考 +进一步压榨性能和存储方案思考:Protobuf+ GZip
java·spring boot
毕设源码-郭学长8 小时前
【开题答辩全过程】以 高校教材大管家系统为例,包含答辩的问题和答案
java·spring boot
qq_12498707538 小时前
基于SpringBoot+vue的小黄蜂外卖平台(源码+论文+部署+安装)
java·开发语言·vue.js·spring boot·后端·mysql·毕业设计