zoukankan      html  css  js  c++  java
  • netty

    import com.yd.lbs.gps.acceptor.util.PropertiesUtil;
    import io.netty.bootstrap.ServerBootstrap;
    import io.netty.buffer.PooledByteBufAllocator;
    import io.netty.channel.*;
    import io.netty.channel.nio.NioEventLoopGroup;
    import io.netty.channel.socket.SocketChannel;
    import io.netty.channel.socket.nio.NioServerSocketChannel;
    import io.netty.handler.logging.LogLevel;
    import io.netty.handler.logging.LoggingHandler;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    
    public class Jt808GpsServer {
    
        private static Logger logger = LoggerFactory.getLogger(Jt808GpsServer.class);
    
        public static void main(String[] args) throws InterruptedException {
            int port = PropertiesUtil.GPS_PORT;
            if (args != null && args.length > 0) {
                try {
                    port = Integer.parseInt(args[0]);
                } catch (NumberFormatException e) {
                }
            }
            logger.info("Jt808 gps server port is:{} ", port);
            new Jt808GpsServer().bind(port);
        }
    
        public void bind(int port) throws InterruptedException {
    
            //EventLoopGroup 类似线程组,boss用于监听是否有tcp链接过来;worker用于建立链接并处理
            EventLoopGroup bossGroup = new NioEventLoopGroup();
            //如果不设置参数值,则默认取CPU核心数*2
            EventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .option(ChannelOption.SO_BACKLOG, 100)
                        .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                        .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                        .handler(new LoggingHandler(LogLevel.INFO))
                        .childHandler(new ChannelInitializer<SocketChannel>() {
                            @Override
                            public void initChannel(SocketChannel ch) {
                                ChannelPipeline pipeline = ch.pipeline();
                                pipeline.addLast("decode", new Jt808MessageDecoder());
                                pipeline.addLast("encode", new Jt808MessageEncoder());
                                pipeline.addLast(new Jt808GpsServerHandler());
    
                            }
                        });
                //绑定端口,同步等待成功
                ChannelFuture f = b.bind(port).sync();
                //等待服务器端监听端口关闭
                f.channel().closeFuture().sync();
                logger.info("Jt808 gps server is started up...");
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }
    }
  • 相关阅读:
    [洛谷P4585] [FJOI2015] 火星商店问题
    [bzoj4311] 向量
    [bzoj4977] [Lydsy1708月赛] 跳伞求生
    sdut-1153 C语言实验——求两个整数之中较大者
    sdut_1116
    sdut_1189
    汉诺塔
    二分查找
    类似二分查找算法
    [YTU]_2922(Shape系列-8)
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/6541953.html
Copyright © 2011-2022 走看看