package com.zksy.water.config;

import io.netty.channel.EventLoopGroup;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Slf4j
@Component
public class NettyServer {

	private static Logger logger = LoggerFactory.getLogger(NettyServer.class);
	public static EventLoopGroup acceptor;
	public static EventLoopGroup worker;

	@Autowired
	private NettyServerThread nettyServerThread;

	@PostConstruct
	public void init() {
		new Thread(() -> nettyServerThread.startServer()).start();
		System.out.println("nettyServer启动");
		logger.info("nettyServer启动");
	}

	@PreDestroy
	public void exit() {
		if (acceptor != null) {
			acceptor.shutdownGracefully();
		}
		if (worker != null) {
			worker.shutdownGracefully();
		}
	}
}