package com.xxfc.platform.order.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

/**
 * rabbitmq配置类
 * 过期订单取消延时队列
 * @author zhanghang
 * @date 2019/1/7
 */
@Configuration
public class RabbitUserConfig {

    public static final String ORDER_CANCEL_EXC = "order_cancel_delay_exchange";
    public static final String ORDER_CANCEL_QUE = "order_cancel_delay_queue";
    public static final String ORDER_CANCEL_KEY = "order_cancel_delay_key";

    /**
     * 延时队列交换机
     * 注意这里的交换机类型：CustomExchange 
     * @return
     */
    @Bean
    public CustomExchange delayExchange(){
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(ORDER_CANCEL_EXC,"x-delayed-message",true, false, args);
    }

    /**
     * 延时队列
     * @return
     */
    @Bean
    public Queue delayQueue(){
        return new Queue(ORDER_CANCEL_QUE,true);
    }

     /**
     * 给延时队列绑定交换机
     * @return
     */
    @Bean
    public Binding cfgDelayBinding(Queue cfgDelayQueue, CustomExchange cfgUserDelayExchange){
        return BindingBuilder.bind(cfgDelayQueue).to(cfgUserDelayExchange).with(ORDER_CANCEL_KEY).noargs();
    }
}

