springboot整合rabbitmq实现延迟队列

延迟队列是指按照生产者发消息时设置的延迟时间大小顺序接收消息。

①实现延迟队列需要先安装rabbitmq的延迟队列插件。

在官网上下载 https://www.rabbitmq.com/community-plugins.html插件。
ftp上传rabbitmq_delayed_message_exchange插件到根目录。

cp rabbitmq_delayed_message_exchange插件  /usr/lib/rabbitmq/lib/rabbitmq_server-3.8.8/plugins

启动插件

rabbitmq-plugins enable rabbitmq_delayed_message_exchange

②判断延迟插件是否生效

rabbitmq的15672面板的交换机类型会出现延迟交换机类型:x-delayed-message
springboot整合rabbitmq实现延迟队列_第1张图片
注意:延迟队列其实延迟功能是交换机实现的。交换机到延迟时间发送消息给队列,队列都是先进先出,

③yml配置文件

spring:
  rabbitmq:
    host: 192.168.64.137
    port: 5672
    username: username
    password: password
    virtual-host: /

④编写配置类代码

package com.cherry.rabbitmqttl.config;

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

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

/**
 * 延迟队列
 */
@Configuration
public class DelayedQueueConfig {
    public static final String DELAYED_QUEUE_NAME = "delayed.queue";
    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";
    @Bean
    public Queue delayedQueue() {
        return new Queue(DELAYED_QUEUE_NAME);
    }
    //自定义交换机 我们在这里定义的是一个延迟交换机
    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> args = new HashMap<>();
        //自定义交换机的类型
        args.put("x-delayed-type", "direct");
        return new CustomExchange(DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false,
                args);
    }
    @Bean
    public Binding bindingDelayedQueue(@Qualifier("delayedQueue") Queue queue,
                                       @Qualifier("delayedExchange") CustomExchange
                                               delayedExchange) {
        return
                BindingBuilder.bind(queue).to(delayedExchange).with(DELAYED_ROUTING_KEY).noargs();
    }
}

⑤生产者代码

package com.cherry.rabbitmqttl.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 发送延迟消息
 */
@Slf4j
@RequestMapping("ttl")
@RestController
public class SendMsgController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    // 基于插件
    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";
    @GetMapping("sendDelayMsg/{message}/{delayTime}")
    public void sendMsg(@PathVariable String message,@PathVariable Integer delayTime) {
        rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME, DELAYED_ROUTING_KEY, message,
                correlationData ->{
                    correlationData.getMessageProperties().setDelay(delayTime);
                    return correlationData;
                });
        log.info(" 当 前 时 间 : {}, 发送一条延迟 {} 毫秒的信息给队列 delayed.queue:{}", new
                Date(),delayTime, message);
    }
}

⑥消费者

package com.cherry.rabbitmqttl.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *
 */
@Component
@Slf4j
public class DelayQueueConsumer {
    public static final String DELAYED_QUEUE_NAME = "delayed.queue";


    @RabbitListener(queues = DELAYED_QUEUE_NAME)
    public void receiveDelayedQueue(Message message){
        String msg = new String(message.getBody());
        log.info("当前时间:{},收到延时队列的消息:{}", new Date().toString(), msg);
    }

}

⑦启动类

package com.cherry.rabbitmqttl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RabbitmqttlApplication {

    public static void main(String[] args) {
        SpringApplication.run(RabbitmqttlApplication.class, args);
    }

}

你可能感兴趣的:(java-rabbitmq,rabbitmq,spring,boot)