Netty 4 外发消息处理器关闭连接

4
我正在尝试使用Netty 4中的自定义出站消息处理程序,但似乎无法使其正常工作。该处理程序只是记录一条语句,并添加到通道管道的底部。我的理解是,这些处理程序在发出写操作后按照从下到上的顺序依次调用。自定义处理程序的想法是,在任何其他出站消息处理程序之前执行它。
不幸的是,当我将此日志处理程序添加到管道中时,我看到日志记录语句,但然后Netty似乎立即关闭了连接。以下是我的通道初始化程序和出站处理程序代码。
HttpOutboundHandler.java
public class HttpOutboundHandler extends ChannelOutboundMessageHandlerAdapter<DefaultFullHttpResponse> {
    private static final Logger logger = LoggerFactory.getLogger(HttpOutboundHandler.class);

    @Override
    public void flush(ChannelHandlerContext context, DefaultFullHttpResponse response)
            throws Exception {
        logger.debug("Executing outbound handler.");
    }
}

HttpChannelInitializer.java

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("aggregator", new HttpObjectAggregator(1048576);
    pipeline.addLast("compressor", new HttpContentCompressor(gzipLevel));
    pipeline.addLast("outboundHandler", outboundHandler);
    pipeline.addLast(eventExecutorGroup, "inboundHandler", inboundHandler);
}

最后,这是记录器的输出结果。
[DEBUG] (Slf4JLogger:71) - [id: 0xbddf00cf, /0:0:0:0:0:0:0:1:57402 => /0:0:0:0:0:0:0:1:8080] ACTIVE
[DEBUG] (HttpOutboundHandler:19) - Executing outbound handler.
[DEBUG] (Slf4JLogger:71) - [id: 0x942993c1, /0:0:0:0:0:0:0:1:57403 :> /0:0:0:0:0:0:0:1:8080] INACTIVE
1个回答

3

为了帮助其他人,我来回答自己的问题。

事实证明,我需要将这个消息添加到下一个出站消息缓冲区中(我认为这会将其传递给链中的下一个处理程序)。我还需要保留这条消息。更新后的代码现在看起来像这样...

public class HttpOutboundHandler extends ChannelOutboundMessageHandlerAdapter<DefaultFullHttpResponse> {
    private static final Logger logger = LoggerFactory.getLogger(HttpOutboundHandler.class);

    @Override
    public void flush(ChannelHandlerContext context, DefaultFullHttpResponse response)
            throws Exception {
        logger.debug("Executing outbound handler.");
        ChannelHandlerUtil.addToNextOutboundBuffer(context, response.retain());
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接