1. @Override
    2. protected void doOpen() throws Throwable {
    3. bootstrap = new ServerBootstrap();
    4. bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("NettyServerBoss", true));
    5. workerGroup = new NioEventLoopGroup(getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
    6. new DefaultThreadFactory("NettyServerWorker", true));
    7. final NettyServerHandler nettyServerHandler = new NettyServerHandler(getUrl(), this);
    8. channels = nettyServerHandler.getChannels();
    9. bootstrap.group(bossGroup, workerGroup)
    10. .channel(NioServerSocketChannel.class)
    11. .childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE)
    12. .childOption(ChannelOption.SO_REUSEADDR, Boolean.TRUE)
    13. .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
    14. .childHandler(new ChannelInitializer<NioSocketChannel>() {
    15. @Override
    16. protected void initChannel(NioSocketChannel ch) throws Exception {
    17. // FIXME: should we use getTimeout()?
    18. int idleTimeout = UrlUtils.getIdleTimeout(getUrl());
    19. NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this);
    20. ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug
    21. .addLast("decoder", adapter.getDecoder())
    22. .addLast("encoder", adapter.getEncoder())
    23. .addLast("server-idle-handler", new IdleStateHandler(0, 0, idleTimeout, MILLISECONDS))
    24. .addLast("handler", nettyServerHandler);
    25. }
    26. });
    27. // bind
    28. ChannelFuture channelFuture = bootstrap.bind(getBindAddress());
    29. channelFuture.syncUninterruptibly();
    30. channel = channelFuture.channel();
    31. }