package com.muyu.breakdown.config;

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

import java.util.HashMap;

/**
 * 绑定交换机与队列
 * */
@Configuration
public class RabbitMQDirectConfig {
    /**
     * 与fanout发布订阅模式不同的是 Direct 需要在Binding规定对应的 路由名称
     * */
    // 1.生命注册fanout模式的交换机
    @Bean
    public DirectExchange directExchange(){
        //1.fanout模式的路由名称 2.是否持久化 3. 是否自动删除
        return new DirectExchange("direct",true,false);
    }
    // 2.生命队列 sms.fanout.queue email.fanout.queue,duanxin.fanout.queue
    @Bean
    public Queue directduanxinQueue(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-delayed-letter-exchange","direct");
        map.put("x-delayed-routing-key","directduanxin");
        map.put("x-message-ttl",10000);
        return QueueBuilder.durable("directduanxin").withArguments(map).build();
    }


    @Bean
    public Queue directemailQueue(){
        return new Queue("directemail",false);
    }

    @Bean
    public Queue directsmsQueue(){
        return new Queue("directsms",false);
    }

    // 3.完成绑定关系(队列和交换机完成绑定关系
    @Bean
    public Binding directduanxinExchange(){
        return BindingBuilder.bind(directduanxinQueue()).to(directExchange()).with("directduanxin");
    }


    @Bean
    public Binding directemailExchange(){
        return BindingBuilder.bind(directemailQueue()).to(directExchange()).with("directemail");
    }

    @Bean
    public Binding directsmsExchange(){
        return BindingBuilder.bind(directsmsQueue()).to(directExchange()).with("directsms");
    }

    // 4.生命注册fanout模式的交换机
}
