package net.virtuemed.jt808.server; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.util.ResourceLeakDetector; import io.netty.util.concurrent.EventExecutorGroup; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; /** * @Author: Zpsw * @Date: 2019-05-15 * @Description: * @Version: 1.0 */ @Slf4j @Component public class NettyTcpServer { @Value("${netty.port}") private int port; @Autowired @Qualifier("bossGroup") private NioEventLoopGroup bossGroup; @Autowired @Qualifier("workerGroup") private NioEventLoopGroup workerGroup; @Autowired @Qualifier("businessGroup") private EventExecutorGroup businessGroup; @Autowired private JT808ChannelInitializer jt808ChannelInitializer; /** * 启动Server * * @throws InterruptedException */ @PostConstruct public void start() throws InterruptedException { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(jt808ChannelInitializer) .option(ChannelOption.SO_BACKLOG, 1024) //服务端可连接队列数,对应TCP/IP协议listen函数中backlog参数 .childOption(ChannelOption.TCP_NODELAY, true)//立即写出 .childOption(ChannelOption.SO_KEEPALIVE, true);//长连接 ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.SIMPLE);//内存泄漏检测 开发推荐PARANOID 线上SIMPLE ChannelFuture channelFuture = serverBootstrap.bind(port).sync(); if (channelFuture.isSuccess()) { log.info("TCP服务启动完毕,port={}", this.port); } } /** * 销毁资源 */ @PreDestroy public void destroy() { bossGroup.shutdownGracefully().syncUninterruptibly(); workerGroup.shutdownGracefully().syncUninterruptibly(); businessGroup.shutdownGracefully().syncUninterruptibly(); log.info("关闭成功"); } }