SpringBoot 整合RabbitMQ 之延迟队列实验

在Spring Boot中整合RabbitMQ并实现延迟队列的功能,可以按照以下步骤进行:

  1. 添加依赖:在pom.xml文件中添加RabbitMQ和Spring AMQP相关的依赖。
java 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  1. 配置RabbitMQ连接信息:在application.properties或application.yml文件中配置RabbitMQ的连接信息,包括host、port、username、password等。
java 复制代码
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
  1. 创建消息发送者:创建一个消息发送者类,用于发送消息到RabbitMQ。
java 复制代码
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {
    
    @Autowired
    private AmqpTemplate amqpTemplate;
    
    public void sendMessage(String message, long delayTime) {
        amqpTemplate.convertAndSend("exchangeName", "routingKey", message, message -> {
            message.getMessageProperties().setDelay((int) delayTime);
            return message;
        });
    }
}
  1. 创建消息接收者:创建一个消息接收者类,用于监听RabbitMQ中的消息。
java 复制代码
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageReceiver {
    
    @RabbitListener(queues = "queueName")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}
  1. 创建延迟队列配置类:创建一个延迟队列的配置类,用于声明交换机、队列和绑定关系。
java 复制代码
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DelayedQueueConfig {
    
    @Bean
    public Queue queue() {
        return new Queue("queueName");
    }
    
    @Bean
    public CustomExchange exchange() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange("exchangeName", "x-delayed-message", true, false, args);
    }
    
    @Bean
    public Binding binding(Queue queue, CustomExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("routingKey").noargs();
    }
}

以上步骤完成后,就实现了Spring Boot与RabbitMQ的整合,并且可以使用延迟队列发送和接收消息。在发送消息时,通过设置delayTime参数来设置消息的延迟时间。在接收消息时,通过@RabbitListener注解来监听指定的队列,并处理接收到的消息。

相关推荐
不平衡的叉叉树4 小时前
Springboot+Mockito简单使用单元测试
java·spring boot·单元测试
山东点狮信息科技有限公司4 小时前
企业级开源OA系统推荐
vue.js·spring boot·性能优化·系统架构·开源
万亿少女的梦1684 小时前
基于Spring Boot与MySQL的乡镇群众服务系统设计与实现
java·spring boot·mysql·权限管理·前后端分离
时代的狂6 小时前
RabbitMQ 面试问答
分布式·面试·rabbitmq
杨运交8 小时前
[049][Crypto模块]前后端混合加密API实战:基于Spring Boot的AES+RSA安全传输方案
spring boot·后端·安全
William Dawson8 小时前
【国产化改造实战|Spring Boot对接华为云MRS Redis Kerberos认证终极踩坑指南】
spring boot·redis·华为云
宠友信息9 小时前
消息撤回与已读状态如何在即时通讯源码中统一管理
java·spring boot·websocket·mysql·uni-app
小小放舟、9 小时前
Windows 本地安装 Elasticsearch 8.10.0 与 IK 分词器(2026)
java·大数据·windows·spring boot·elasticsearch·搜索引擎·全文检索
半夜修仙9 小时前
Spring集成邮箱功能
java·开发语言·spring boot·spring·rabbitmq·github·maven
宠友信息9 小时前
从重复请求到订单同步看内容社区源码的处理思路
java·spring boot·redis·后端·mysql·spring