package com.ruoyi.rabbitmq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.ruoyi.rabbitmq.constant.GeneralConstant.CustomTestConstant.*;

/**
 * rabitmq 配置
 */
@Configuration
@Slf4j
public class RabbitConfig {


    /**
     * 针对消费者配置
     * 1. 设置交换机类型
     * 2. 将队列绑定到交换机
     FanoutExchange: 将消息分发到所有的绑定队列，无routingkey的概念
     HeadersExchange ：通过添加属性key-value匹配
     DirectExchange:按照routingkey分发到指定队列
     TopicExchange:多关键字匹配
     */

    //region

    /**
     * 自定义测试MQ交换机
     *
     * @return
     */
    @Bean
    public DirectExchange customTestExchange() {
        return new DirectExchange(CUSTOM_TEST_EXCHANGE);
    }

    /**
     * 自定义测试MQ队列
     *
     * @return
     */
    @Bean
    public Queue customTestQueue() {
        return new Queue(CUSTOM_TEST_QUEUE, true); //队列持久
    }

    /**
     * 自定义测试MQ与交换机绑定
     *
     * @return
     */
    @Bean
    public Binding customTestBinding() {
        return BindingBuilder.bind(customTestQueue()).to(customTestExchange()).with(CUSTOM_TEST_ROUTER);
    }

    //endregion

}
