package com.docus.server.common.netty.event;

import com.docus.server.common.netty.server.CollectorChannelCacheMap;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class StartUpEventListener {

    @Resource
    private CollectorChannelCacheMap collectorChannelCacheMap;


    @EventListener
    @Async("threadPoolExecutor")
    public void startUp(StartUpEvent startUpEvent) {
        while (true) {
            String collectorId = startUpEvent.getMessage();
            Channel channel = collectorChannelCacheMap.get(collectorId);
            log.info("等待采集器={}启动中", collectorId);
            if (null != channel && channel.isOpen()) {
                log.info("等待采集器={}启动成功并注册到终端", collectorId);
                collectorChannelCacheMap.writeAndFlush(collectorId, startUpEvent.getCommMsg());
                break;
            }
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
