netty

In Netty 4, what's the difference between ctx.close and ctx.channel.close?


Is there any difference? Is ctx.close just a shorter version of ctx.channel.close?


Solution

  • Let's say we have three handlers in the pipeline, and they all intercept the close() operation, and calls ctx.close() in it.

    ChannelPipeline p = ...;
    p.addLast("A", new SomeHandler());
    p.addLast("B", new SomeHandler());
    p.addLast("C", new SomeHandler());
    ...
    
    public class SomeHandler extends ChannelOutboundHandlerAdapter {
        @Override
        public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
            ctx.close(promise);
        }
    }
    

    So, when you should use Channel.close() and ChannelHandlerContext.close()? The rule of thumb is: