2014-01-20 20 views

答えて

22

パイプラインに3つのハンドラがあり、それらがすべて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スレッドではありませんバックグラウンドスレッドを持っている、とあなたがそのスレッドからの接続をクローズします。)
+0

、CTX .channel.closeを使用する必要がありますか? ctx.closeを使用すべき例を挙げることができますか? –

+1

一般に、ChannelPipeline内の "後の" ChannelHandlerがcloseイベントを気にしないことが分かっているときは、ctx.close()を使用できます。 –

+0

回答が更新されました。 – trustin

17

ctx.close()はChannelHandlerContextのポイントからChannelPipelineを流れ始め、ctx.channel()。close()は常にChannelPipelineの末尾から開始します。

関連する問題