1 ServerBootstrap bootstrap = new ServerBootstrap(); 2 try { 3 bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) 4 .childHandler(new ChannelInitializer() { 5 @Override 6 protected void initChannel(Channel ch) throws Exception { 7 ch.pipeline().addLast( 8 new HttpServerCodec(),new HttpObjectAggregator(LOCALPORT), // 將多個消息轉換爲單一的一個FullHttpRequest 9 new HttpRequestDecoder(), 10 new ServcerHandler() // 本身實現的Handler 11 ); 12 } 13 }).childOption(ChannelOption.AUTO_READ, false) 14 .bind(LOCALPORT).sync().channel().closeFuture().sync();
由於設置了bootstrap
childOption(ChannelOption.AUTO_READ, false)ide
因此在執行完spa
public void channelActive(ChannelHandlerContext ctx)code
方法後,不會自動執行blog
public void channelRead(ChannelHandlerContext ctx, Object msg) 方法;ip
須要在channelActive中添加這行語句纔會調用channelRead方法:it
ctx.channel().read();pip