package com.fzm.mall.server.front.mq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

/**
 * @author tdz
 * @date 2021/03/04 14:51
 */
@Configuration
public class RabbitOrderConf {

    @Bean
    public Queue delayOrderQueue() {
        Map<String, Object> params = new HashMap<>(2);
        // x-dead-letter-exchange 声明了队列里的死信转发到的DLX名称，
        params.put("x-dead-letter-exchange", MqConst.EXCHANGE_MALL_ORDER);
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的 routing-key 名称。
        params.put("x-dead-letter-routing-key", MqConst.KEY_MALL_ORDER);
        return new Queue(MqConst.QUEUE_MALL_ORDER_DELAY, true, false, false, params);
    }

    @Bean
    public DirectExchange delayOrderExchange() {
        return new DirectExchange(MqConst.EXCHANGE_MALL_ORDER_DELAY);
    }

    @Bean
    public Binding dlxOrderBinding() {
        return BindingBuilder.bind(delayOrderQueue()).to(delayOrderExchange()).with(MqConst.KEY_MALL_ORDER);
    }

    @Bean
    public Queue orderQueue() {
        return new Queue(MqConst.QUEUE_MALL_ORDER);
    }

    @Bean
    public TopicExchange orderTopicExchange() {
        return new TopicExchange(MqConst.EXCHANGE_MALL_ORDER);
    }

    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderQueue()).to(orderTopicExchange()).with(MqConst.KEY_MALL_ORDER);
    }
}
