未调用 netty channelDuplexHandler 写入

netty channelDuplexHandler write is not called

ChannelDuplexHandler 写入方法应该在它是最后一个处理程序时调用,但实际上并非如此。

来自 ChannelPipeline,

 ChannelPipeline p = ...;
 p.addLast("1", new InboundHandlerA());
 p.addLast("5", new InboundOutboundHandlerX());

入站和出站事件的评估顺序可以分别为 1,5 和 5。

所以我想当channelRead中的ctx.write()被调用时,下面class中的write应该被调用:

@ChannelHandler.Sharable
@Component
public class InboundOutboundHandlerX extends ChannelDuplexHandler {


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ctx.write(msg); // I have also tried writeAndFlush(msg)
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        ctx.write(msg, promise);  
    }

}

是否有特殊技巧来调用最后一个双工处理程序写入方法?

感谢 Norman 的 slides,这是因为 ctx.write()writeAndFlush() 将从下一个处理程序开始,而不是当前处理程序,而 ctx.channel().write() 将从顶部开始pipeline.

的(或尾巴,如果方向感更好的话)