笔记

1、服务端的BizHandler继承ChannelHandlerAdapter,以便让服务器把客户端请求的数据发送回去(服务器端有可能在channelRead方法返回前还没有写完数据,因此不能让它自动release)。
2、ctx.close 和 ctx.channel.close 区别

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);
}
}
Channel.close() will trigger C.close(), B.close(), A.close(), and then close the channel.
ChannelPipeline.context("C").close() will trigger B.close(), A.close(), and then close the channel.
ChannelPipeline.context("B").close() will trigger A.close(), and then close the channel.
ChannelPipeline.context("A").close() will close the channel. No handlers will be called.
So, when you should use Channel.close() and ChannelHandlerContext.close()? The rule of thumb is:

If you are writing a ChannelHandler and wanna close the channel in the handler, call ctx.close().
If you are closing the channel from outside the handler (e.g. you have a background thread which is not an I/O thread, and you want to close the connection from that thread.)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,490评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 10,087评论 0 23
  • 推荐给各位科研工作者一本好书:writing science。这本书不同于其他教写作的书,不是从写作的角度教你具体...
    石博士阅读 5,443评论 1 17
  • 相传古希腊雕刻家皮格马利翁深深地爱上了自己用象牙雕刻的美丽少女,并希望少女能够变成活生生的真人。他的真挚的爱感动了...
    画画的若水阅读 1,005评论 0 2
  • 最近最开心的事就是看姐姐的朋友圈。看她和小米还有爸爸。看字里行间里爸爸生活状态。虽然我不在他们身边,但看他们的朋友...
    小木丁羊阅读 248评论 0 0