ChannelDuplexHandler write method should be called when it is the last handler, but in practice it is not.

From ChannelPipeline,

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

the evaluation order of an inbound and a outbound event could be 1,5 and 5 respectively.

So I thought write in the following class should be called when ctx.write() in channelRead is called:

@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); } } 

Is there a special trick to get the last duplex handler write method called?

1 Answer

Thanks to Norman's slides, this is because ctx.write() or writeAndFlush() will start from the next handler, not the current, while ctx.channel().write() will start from the top (or the tail if that fits a better sense of direction) of pipeline.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.