package cn.reghao.tnb.data.app.mq;

import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2023-12-15 14:42:47
 */
@Component
public class MessageListenerContainerFactory {
    private final ConnectionFactory connectionFactory;

    public MessageListenerContainerFactory(ConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    public MessageListenerContainer createMessageListenerContainer(String queueName) {
        SimpleMessageListenerContainer mlc = new SimpleMessageListenerContainer(connectionFactory);
        mlc.addQueueNames(queueName);
        return mlc;
    }

    // 将 dmaster 队列通过 routeKey dmaster 绑定到 amq.direct 交换机
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "tnb.data", durable = "true"),
            key = "tnb.data",
            exchange = @Exchange(value = "amq.direct"))
    )
    public void dmasterQueueConsumer(@Payload String msg) {
    }

    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "tnb.log", durable = "true"),
            exchange = @Exchange(value = "rabbit.tnb.log"))
    )
    public void logQueueConsumer(@Payload String msg) {
    }*/
}
