有什么区别吗?是 ctx.close
只是 ctx.channel.close
的较短版本?
最佳答案
假设我们在管道中有三个处理程序,它们都拦截 close()
操作,并调用 ctx.close()
在里面。
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);
}
}
Channel.close()
将触发C.close()
, B.close()
, A.close()
,然后关闭 channel 。 ChannelPipeline.context("C").close()
将触发B.close()
, A.close()
,然后关闭 channel 。 ChannelPipeline.context("B").close()
将触发A.close()
,然后关闭 channel 。 ChannelPipeline.context("A").close()
将关闭 channel 。不会调用任何处理程序。 所以,什么时候应该使用
Channel.close()
和 ChannelHandlerContext.close()
?经验法则是:ChannelHandler
如果想关闭处理程序中的 channel ,请调用 ctx.close()
. 关于netty - 在 Netty 4 中,ctx.close 和 ctx.channel.close 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21240981/