netty SimpleChannelInboundHandler中自动读取的正确用法

netty SimpleChannelInboundHandler中自动读取的正确用法,netty,Netty,我正在将代码从netty 3.8版迁移到netty 4.1版 我使用netty使用http服务器,下面是http服务器的代码 public class SimpleHTTPServer { public void start() { try { ServerBootstrap b = new ServerBootstrap(); EventLoopGroup bossGroup = new NioEvent

我正在将代码从netty 3.8版迁移到netty 4.1版 我使用netty使用http服务器,下面是http服务器的代码

public class SimpleHTTPServer {

      public void start() {
          try {
              ServerBootstrap b = new ServerBootstrap();
              EventLoopGroup bossGroup = new NioEventLoopGroup();
              EventLoopGroup workerGroup = new NioEventLoopGroup();

              b.group(bossGroup, workerGroup)

                      .channel(NioServerSocketChannel.class)
                      .childHandler(new ChannelInitializer<SocketChannel>() {
                          @Override
                          public void initChannel(SocketChannel ch) {
                              ch.pipeline().addLast("codec", new HttpServerCodec());
                              ch.pipeline().addLast("aggregator",
                                      new HttpObjectAggregator(512 * 1024));
                              ch.pipeline().addLast("request",
                                      new SimpleHTTPHandler());
                          }
                      })
                      .option(ChannelOption.SO_BACKLOG, 128)
                      .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT)
                      .childOption(ChannelOption.AUTO_READ, false)
                      .childOption(ChannelOption.SO_KEEPALIVE, true);

              ChannelFuture f = b.bind(5055).sync();
              f.channel().closeFuture().sync();

          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
公共类SimpleHTTPServer{
公开作废开始(){
试一试{
ServerBootstrap b=新的ServerBootstrap();
EventLoopGroup bossGroup=新的NioEventLoopGroup();
EventLoopGroup workerGroup=新的NioEventLoopGroup();
b、 组(bossGroup、workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(新的通道初始值设定项(){
@凌驾
公共频道(SocketChannel ch){
ch.pipeline().addLast(“codec”,新的HttpServerCodec());
ch.pipeline().addLast(“聚合器”,
新的HttpObjectAggregator(512*1024));
ch.pipeline().addLast(“请求”,
新的SimpleHTTPHandler());
}
})
.option(ChannelOption.SO_BACKLOG,128)
.childOption(ChannelOption.WRITE\u BUFFER\u WATER\u MARK,WriteBufferWaterMark.DEFAULT)
.childOption(ChannelOption.AUTO_READ,false)
.childOption(ChannelOption.SO_KEEPALIVE,true);
ChannelFuture f=b.bind(5055.sync();
f、 通道().closeFuture().sync();
}捕获(例外e){
e、 printStackTrace();
}
}
}
下面是通道处理程序代码

public class SimpleHTTPHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

        protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
            HttpResponseStatus responseStatus = OK;
            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, responseStatus, Unpooled.copiedBuffer("My Netty".getBytes()));
            response.headers().add(request.headers());
            response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
            if (isKeepAlive(request)) {
                response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
            }
            ctx.writeAndFlush(response);
        }

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            super.channelActive(ctx);
            ctx.channel().read();
        }
    }
公共类SimpleHTTPHandler扩展了SimpleChannelInboundHandler{
受保护的无效channelRead0(ChannelHandlerContext ctx,FullHttpRequest请求){
HttpResponseStatus responseStatus=OK;
FullHttpResponse response=newdefaultfullhttpresponse(HTTP_1_1,responseStatus,unmooled.copiedBuffer(“My Netty.getBytes()));
response.headers().add(request.headers());
response.headers().set(CONTENT_LENGTH,response.CONTENT().readableBytes());
如果(isKeepAlive(请求)){
response.headers().set(连接,HttpHeaderValues.KEEP_活动);
}
ctx.writeAndFlush(响应);
}
@凌驾
公共无效channelActive(ChannelHandlerContext ctx)引发异常{
超级通道激活(ctx);
ctx.channel().read();
}
}
如前所述,我在HTTPServer中将AUTO_READ option设置为false,此后,通道消息不会在channelHandler中处理,如这里所述

因此,我手动调用了通道读取。
ChannelOption.AUTO\u READ在此代码中处理是否正确?

我认为在您收到channelRead0(…)中的请求后,您没有再次调用
ctx.READ()
。如果你不打电话,你将不会收到更多的请求,这很可能不是你想要的


另外,您很可能希望将
ctx.channel().read()
替换为
ctx.read()
,这样您就开始让出站事件由当前处理程序处理,而不是让它流经整个管道(如果触发读取的处理程序之后还有更多的处理程序).

channelRead0(…)
中收到请求后,我认为您没有再次调用
ctx.read()
。如果你不打电话,你将不会收到更多的请求,这很可能不是你想要的

另外,您很可能希望将
ctx.channel().read()
替换为
ctx.read()
,这样您就开始让出站事件由当前处理程序处理,而不是让它流经整个管道(如果触发读取的处理程序之后还有更多的处理程序)