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);
    }

}
相关推荐
Persistence___1 小时前
SpringBoot中的拦截器
java·spring boot·后端
嘵奇1 小时前
Spring Boot 跨域问题全解:原理、解决方案与最佳实践
java·spring boot·后端
堕落年代1 小时前
SpringBoot的单体和分布式的任务架构
spring boot·分布式·架构
码农飞哥2 小时前
互联网大厂Java求职面试实战:Spring Boot与微服务场景深度解析
java·数据库·spring boot·安全·微服务·消息队列·互联网医疗
I_itaiit3 小时前
Spring Boot之Web服务器的启动流程分析
spring boot·nettywebserver·httphandler·webhandler
老李不敲代码4 小时前
榕壹云搭子系统技术解析:基于Spring Boot+MySQL+UniApp的同城社交平台开发实践
spring boot·mysql·微信小程序·uni-app·软件需求
养军博客5 小时前
spring boot3.0自定义校验注解:文章状态校验示例
java·前端·spring boot
小赵面校招6 小时前
Spring Boot整合MyBatis全攻略:原理剖析与最佳实践
java·spring boot·mybatis
曼岛_6 小时前
[Java实战]Spring Boot 3 整合 Ehcache 3(十九)
java·spring boot·spring
意倾城6 小时前
Spring Boot 配置文件敏感信息加密:Jasypt 实战
java·spring boot·后端