笔记

作者: 骑忆 | 来源:发表于2018-04-13 09:56 被阅读0次
    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.)

    相关文章

      网友评论

          本文标题:笔记

          本文链接:https://www.haomeiwen.com/subject/xbceixtx.html