package com.steerinfo.ems.websocket.controller;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Shadow
 *  @description:由于@RabbitListener的队列名称只能是常量，所以@RabbitListener监听注解
 *              不能使用，只能使用rabbitmq的api接口
 * @create 2021-10-11 17:24
 * @project xt-ems-api-new
 */
//@Configuration
//@EnableRabbit
@Configuration
@AutoConfigureAfter(RabbitTemplate.class)
public class DirectRabbitMsgReceiveConfig {
//    /**
//     * json序列化
//     * @return
//     */
//    @Bean
//    public Queue helloQueue(){
//        return new Queue("ems_webSocket");
//    }
//
    /**
     * 使用orderMqConsumer的orderShow方法作为listener。
     * @return
     */
    @Bean("orderShowMessageListener")
    MessageListenerAdapter orderShowMessageListener() {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter(new WebSocket());
        //WebSocket实现了ChannelAwareMessageListener类则可以不用指定默认的监听方法
        //messageListenerAdapter.setDefaultListenerMethod("handleMessage1");
        return messageListenerAdapter;
    }
    //
    @Bean("orderShowMessageListenerContainer")
    SimpleMessageListenerContainer orderShowMessageListenerContainer(org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory, @Qualifier("orderShowMessageListener") MessageListenerAdapter listenerAdapter, @Qualifier("createWsDirectQueue") Queue queue) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        //只接收该节点的队列信息
        container.setQueueNames(queue.getName());
        container.setMessageListener(listenerAdapter);
        return container;
    }
}
