Netty channelDuplexHandler Write Is Not Called
Channelduplexhandler Write Method Should Be Called When It Is the Last Handler, but in Practice It Is Not. from Channelpipeline, Channelpipeline P =. .. ; P...
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.