SpringBoot整合RabbitMq实现延时队列

文章详细介绍了如何在SpringBoot项目中整合RabbitMq以实现延时队列功能,主要步骤包括安装rabbitmq_delayed_message_exchange插件,配置SpringBoot应用,创建RabbitConfig类定义队列、交换机和绑定,以及设置消费者监听和发送消息的测试。通过这种方式,可以实现消息按设定延迟时间到达队列并被消费。
摘要由CSDN通过智能技术生成

SpringBoot整合RabbitMq实现延时队列

说明:
RabbitMq通过使用rabbitmq_delayed_message_exchange插件实现延迟消息。
它与TTL方式不同的是,TTL方式存放在死信队列(dalayqueue) 里,它是基于插件存放消息放在延时交换机里面(x-delayed-message exchange)
1. 生产者将消息(msg)和路由键(routekey)发送指定的延时交换机(exchange)上
2. 延时交换机(exchange)存储消息等待消息到期根据路由键(routekey)找到绑定自己的队列
   (queue)并把消息给它
3. 队列(queue)再把消息发送给监听它的消费者(customer)

rabbitmq_delayed_message_exchange 插件安装

1.插件下载

下载地址: https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases
在这里插入图片描述

2.插件安装

将插件拷贝到rabbitmq-server的安装路径: /usr/lib/rabbitmq/lib/rabbitmq_server-3.8.4/plugins

3.启用插件

rabbitmq-plugins list
rabbitmq-plugins enable rabbitmq_delayed_message_exchange

4.重启rabbit-service

systemctl restart rabbitmq-server

SpringBoot项目代码

1. pom.xml中添加RabbitMQ

    <!-- MQ依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
        <version>3.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>3.0.2</version>
    </dependency>

2.application.yml中添加rabbitMq的配置

spring:
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: root
    password: 123456
    virtual-host: /svr

3.添加RabbitConfig类

application.yml中请添加自定义的queue、exchange、delayRoutingKey

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.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
@Configuration
public class RabbitConfig {
    
    @Value("${queue.demo.delayQueue}")
    private String delayQueue;
    
    @Value("${queue.demo.delayExchange}")
    private String delayExchange;
    
    @Value("${queue.demo.delayRoutingKey}")
    private String delayRoutingKey;

    @Bean
    public Queue queue() {
        return new Queue(delayQueue, true);
    }

    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>(16);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(delayExchange, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding binding(Queue queue, CustomExchange delayExchange) {
        return BindingBuilder.bind(queue).to(delayExchange).with(delayRoutingKey).noargs();
    }
}

4.添加消费者类监听消息处理

使用推消息模式接收延迟队列的广播

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MsgListener {

    @RabbitListener(queues = {"${queue.demo.delayQueue}"})
    public void msgConsumer(String msg){
        System.out.println("接受到的消息:" + msg);
    }
}

5.发送消息测试

1.添加自定义工具类MsgPublishService
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MsgPublishService {

   @Autowired
   private RabbitTemplate rabbitTemplate;

   /**
    * 发送延时消息
    *
    * @param delayExchange
    * @param delayRoutingKey
    * @param message
    * @param delayTimes
    */
   public void sendTtlMsg(String delayExchange, String delayRoutingKey, String message, long delayTimes) {
      try {
         rabbitTemplate.convertAndSend(delayExchange, delayRoutingKey, message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
               message.getMessageProperties().setHeader("x-delay", delayTimes);
               return message;
            }
         });
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
2.编写测试代码
import com.example.rabbitmqdemo.service.MsgPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @Autowired
    private MsgPublishService msgPublishService;

    @Value("${queue.delayExchange}")
    private String delayExchange;

    @Value("${queue.delayRoutingKey}")
    private String delayRoutingKey;

    @GetMapping("sendMsg")
    public void pushToRabbitMq(@RequestParam("msg") String msg,@RequestParam("delayTime") Long delayTime){
        msgPublishService.sendTtlMsg(delayExchange,delayRoutingKey,msg,delayTime);
    }
}
测试结果
发送请求,延迟6s后消费消息
http://localhost:8080/sendMsg?msg=nihao&delayTime=6000
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值