阅读背景:

用netty4实现http服务器

来源:互联网 
public class HttpServer {
    private static final Logger logger = LogManager.getLogger("default");

    public static void main(String[] args) throws InterruptedException, IOException {
        try {
            MetricKafkaHttpServer server = new MetricKafkaHttpServer();
            server.run();
        } finally {
            logger.error("Server shutting down.");
        }
    }

    public void run() throws InterruptedException {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(workerGroup, bossGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline()
                                    .addLast("inboundDecoder", new HttpRequestDecoder())
                                    .addLast("outboundEncoder", new HttpResponseEncoder()) // 1
                                    .addLast("inboundAggregator", new HttpObjectAggregator(50 * 1024 * 1024))
                                    .addLast("inboundHandler", new HttpRequestHandler());
                        }
                    })
                    //TODO options
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childOption(ChannelOption.SO_KEEPALIVE, true); // 2

            ChannelFuture f = b.bind(ServerProperties.PORT).sync();
            logger.info("Server started in port " + ServerProperties.PORT + ".");

            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}public class HttpServer {
    private static fi



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: