package com.ruoyi.rabbitmq.config;

import com.ruoyi.rabbitmq.constant.DelayedConstant;
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;

import static com.ruoyi.rabbitmq.constant.DelayedConstant.CustomTestDelayedConstant.*;

@Configuration
public class RabbitMqDelayedConfig {


    //region 自定义延迟MQ

    /**
     * 自定义延迟MQ队列
     *
     * @return
     */
    @Bean
    public Queue customTestDelayedQueue() {
        return new Queue(CUSTOM_TEST_DELAYED_QUEUE);
    }

    /**
     * 定义一个支付过期延迟交换机
     *
     * @return
     */
    @Bean
    public CustomExchange customTestDelayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(CUSTOM_TEST_DELAYED_EXCHANGE, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到这个延迟交换机上
     *
     * @param queue
     * @param customExchange
     * @return
     */
    @Bean
    public Binding customTestDelayBinding(@Qualifier("customTestDelayedQueue") Queue queue,
                                    @Qualifier("customTestDelayExchange") CustomExchange customExchange) {
        return BindingBuilder.bind(queue).to(customExchange).with(CUSTOM_TEST_DELAYED_ROUTING).noargs();
    }

    //endregion

}
