码迷,mamicode.com
首页 > Web开发 > 详细

netty

时间:2017-03-13 12:56:55      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:string   final   try   void   dex   channel   ...   blog   loop   

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();
        }
    }
}

 

netty

标签:string   final   try   void   dex   channel   ...   blog   loop   

原文地址:http://www.cnblogs.com/tonggc1668/p/6541953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!