在Netty 4中,什么';ctx.close和ctx.channel.close的区别是什么?
有什么区别吗?是否在Netty 4中,什么';ctx.close和ctx.channel.close的区别是什么?,netty,Netty,有什么区别吗?是否ctx.close只是ctx.channel.close的一个较短版本?ctx.close()从ChannelHandlerContext的点开始流经ChannelPipeline,而ctx.channel().close()将始终从ChannelPipeline的尾部开始。假设我们有三个处理程序在管道中,它们都拦截close()操作,并在其中调用ctx.close() ChannelPipeline p = ...; p.addLast("A", new SomeHandle
ctx.close
只是ctx.channel.close
的一个较短版本?ctx.close()从ChannelHandlerContext的点开始流经ChannelPipeline,而ctx.channel().close()将始终从ChannelPipeline的尾部开始。假设我们有三个处理程序在管道中,它们都拦截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()
将触发ChannelPipeline.context(“C”).close()
,B.close()
,然后关闭频道A.close()
将触发ChannelPipeline.context(“B”).close()
,然后关闭频道A.close()
ChannelPipeline.context(“A”).close()将关闭该频道。不会调用任何处理程序
Channel.close()
和ChannelHandlerContext.close()
?经验法则是:
- 如果您正在编写
并希望关闭处理程序中的通道,请调用ChannelHandler
ctx.close()
- 如果您正在从处理程序外部关闭通道(例如,您有一个不是I/O线程的后台线程,并且您希望从该线程关闭连接。)
@Override
,而且这些要点对我来说没有意义@相信我,我错过了什么吗?Thanks@Aaron谢谢你的报道。它应该是ChannelOutboundHandlerAdapter
。