package cn.reghao.tnb.message.app.rabbit.listener;

import cn.reghao.tnb.message.app.rabbit.listener.WsMessageListener;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;

/**
 * @author reghao
 * @date 2024-11-28 15:42:16
 */
//@Component
public class RabbitListenerConfig {
    private final ConnectionFactory connectionFactory;

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

    public MessageListenerContainer addQueueListener(String queueName, MessageListener listener) {
        SimpleMessageListenerContainer mlc = new SimpleMessageListenerContainer(connectionFactory);
        mlc.addQueueNames(queueName);
        mlc.setupMessageListener(listener);
        return mlc;
    }

    @PostConstruct
    public void init() {
        // 监听并处理 tnb.message.websocket 队列的消息
        MessageListenerContainer mlc = addQueueListener("tnb.message.websocket" , new WsMessageListener());
        mlc.start();
    }
}
