package com.zy.acs.gateway; 
 | 
  
 | 
  
 | 
import com.zy.acs.gateway.config.SystemProperties; 
 | 
import io.netty.bootstrap.ServerBootstrap; 
 | 
import io.netty.buffer.PooledByteBufAllocator; 
 | 
import io.netty.channel.Channel; 
 | 
import io.netty.channel.ChannelOption; 
 | 
import io.netty.channel.EventLoopGroup; 
 | 
import io.netty.channel.nio.NioEventLoopGroup; 
 | 
import io.netty.channel.socket.nio.NioServerSocketChannel; 
 | 
import io.netty.util.ResourceLeakDetector; 
 | 
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; 
 | 
  
 | 
/** 
 | 
 * TCP/IP协议端口 
 | 
 * Netty Server 引导类 
 | 
 * @author Vincent 
 | 
 */ 
 | 
@Component 
 | 
public class OnlineServer { 
 | 
  
 | 
    private static final Logger log = LoggerFactory.getLogger(OnlineServer.class); 
 | 
  
 | 
    private final HandlerInitializer handlerInitializer; 
 | 
    private final SystemProperties systemProperties; 
 | 
    private Channel channel; 
 | 
    private ServerBootstrap bootstrap; 
 | 
    private EventLoopGroup bossGroup; 
 | 
    private EventLoopGroup workerGroup; 
 | 
  
 | 
    @Autowired 
 | 
    public OnlineServer(SystemProperties systemProperties, HandlerInitializer handlerInitializer) { ; 
 | 
        this.systemProperties = systemProperties; 
 | 
        this.handlerInitializer = handlerInitializer; 
 | 
    } 
 | 
  
 | 
    { 
 | 
        bootstrap = new ServerBootstrap(); 
 | 
        bossGroup = new NioEventLoopGroup(1); 
 | 
        workerGroup = new NioEventLoopGroup(); 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     *  tcp server init 
 | 
     */ 
 | 
    @PostConstruct 
 | 
    public void serverStart() throws Exception { 
 | 
  
 | 
        bootstrap.group(bossGroup, workerGroup) 
 | 
                .channel(NioServerSocketChannel.class) 
 | 
                .childHandler(handlerInitializer) 
 | 
                .option(ChannelOption.SO_BACKLOG, systemProperties.getBacklog()) 
 | 
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) 
 | 
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) 
 | 
                .childOption(ChannelOption.SO_KEEPALIVE, systemProperties.isKeepAlive()) 
 | 
                .childOption(ChannelOption.SO_SNDBUF, systemProperties.getSndbuf()) 
 | 
                .childOption(ChannelOption.SO_RCVBUF, systemProperties.getRcvbuf()); 
 | 
  
 | 
        ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED); 
 | 
  
 | 
        log.info("TCP server started successfully, port:{}", systemProperties.getPort()); 
 | 
  
 | 
        channel = bootstrap.bind(systemProperties.getPort()).sync().channel(); 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * tcp server stop 
 | 
     */ 
 | 
    @PreDestroy 
 | 
    public void destroy() { 
 | 
        if (channel != null && channel.isActive()) { 
 | 
            channel.close(); 
 | 
        } 
 | 
        if (bossGroup != null) { 
 | 
            bossGroup.shutdownGracefully(); 
 | 
        } 
 | 
        if (workerGroup != null) { 
 | 
            workerGroup.shutdownGracefully(); 
 | 
        } 
 | 
        log.info("TCP server stopped successfully, port: {}", systemProperties.getPort()); 
 | 
    } 
 | 
  
 | 
} 
 |