2016-05-17 18 views
0

Netty TCPサーバーのタイムアウトの設定に関する質問があります。今、私ははこのような TIMOUT接続設定:netty - TCPサーバーのタイムアウトを設定する

serverBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 20000); 

これは良いともすべて、動作しているようです。今は、サーバー側で「読み取りタイムアウト」を定義することが可能かどうか疑問です。考えられるのは、読み取りタイムアウトが経過するとサーバーワーカースレッドが中断され、他のタスクで利用できるようになることです。私は次のように読み取りタイムアウトを設定しようとすると、私は、「サポートされていないチャネルオプション」起動時に警告を得る:

serverBootstrap.childOption(ChannelOption.SO_TIMEOUT, 30000); 

のサーバ側で「読み取り/処理のタイムアウト」を達成する方法はあります物事?どんな助けもありがとうございます。

種類よろしく、 マイケル

答えて

2

あなたのパイプラインの最初の位置にReadTimeoutHandlerを追加します。

http://netty.io/4.0/api/io/netty/handler/timeout/ReadTimeoutHandler.html

public class ReadTimeoutHandler extends ChannelInboundHandlerAdapter 

// Raises a ReadTimeoutException when no data was read within a certain period of time. 

// The connection is closed when there is no inbound traffic 
// for 30 seconds. 

public class MyChannelInitializer extends ChannelInitializer<Channel> { 
    public void initChannel(Channel channel) { 
     channel.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(30); 
     channel.pipeline().addLast("myHandler", new MyHandler()); 
    } 
} 

// Handler should handle the ReadTimeoutException. 
public class MyHandler extends ChannelDuplexHandler { 
    @Override 
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) 
      throws Exception { 
     if (cause instanceof ReadTimeoutException) { 
      // do something 
     } else { 
      super.exceptionCaught(ctx, cause); 
     } 
    } 
} 

ServerBootstrap bootstrap = ...; 
... 
bootstrap.childHandler(new MyChannelInitializer()); 
... 
+0

ああ、完璧な、何とかReadTimeoutHandlerは私の目を逃れて。ありがとうございました。 –

関連する問題