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

}
相关推荐
JIngJaneIL1 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
从心归零2 小时前
springboot-jpa的批量更新方法
java·spring boot·spring
tiancao2222 小时前
SpringBoot使用Camunda REST Client调用独立部署的Camunda7
spring boot·集成·独立服务器·rest api·camunda7
幽络源小助理4 小时前
SpringBoot+Vue摄影师分享社区源码 – Java项目免费下载 | 幽络源
java·vue.js·spring boot
+VX:Fegn08954 小时前
计算机毕业设计|基于springboot + vue健身房管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
苹果醋35 小时前
iview— Select— Option选中后有空格
运维·vue.js·spring boot·nginx·课程设计
code_std5 小时前
保存文件到指定位置,读取/删除指定文件夹中文件
java·spring boot·后端
汤姆yu5 小时前
基于springboot的热门文创内容推荐分享系统
java·spring boot·后端
星光一影5 小时前
教育培训机构消课管理系统智慧校园艺术舞蹈美术艺术培训班扣课时教务管理系统
java·spring boot·mysql·vue·mybatis·uniapp
武昌库里写JAVA5 小时前
在iview中使用upload组件上传文件之前先做其他的处理
java·vue.js·spring boot·后端·sql