私の読み取りタイムアウトが機能していないのは分かりません。私がしたいのは、ちょうどBlockedQueue<String>
にメッセージを入れるスレッドのために、
を10秒待ってから、タイムアウトしてクライアント上で何らかの応答を返すだけです。ReadTimeoutHandlerのタイムアウトの処理
public class NioAsynChatPipelineFactory implements ChannelPipelineFactory {
private static Timer timer = new HashedWheelTimer();
private final ChannelHandler timeoutHandler = new ReadTimeoutHandler(timer, 10);
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("handler", new NioAsynChatHandler());
pipeline.addLast("timeout", this.timeoutHandler);
return pipeline;
}
}
私のハンドラは次のようになります。
public class NioAsynChatHandler extends SimpleChannelUpstreamHandler{
@Override
public void handleUpstream(
ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
super.handleUpstream(ctx, e);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
System.out.println("Exception");
\\writing some kind of response and closing channel.
}
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Thread thread = new Thread(new ConsmerTask(e.getChannel()));
thread.start();
}
とBlockingQueueのが私の問題は、私はどのexcpetionがタイムアウトに発生することが表示されていないということである応答
public class ConsumerTask implements Runnable{
private Channel channel;
public ConsumerTask(Channel channel){
this.channel = channel;
}
@Override
public void run() {
try{
while(true){
String message = queue.take();
}
} catch(InterruptedException ex){
Thread.currentThread.interrupt();
} finally{
//write something to channel and close it
}
}
を取得するためConsumerTaskの内側に、私はただ待っています。 私は何が間違っていますか?
更新:
public static final BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<String>();
実は私の質問は、それが外部のスレッドで何かを待っている間にタイムアウトにチャネルを閉鎖する方法、より一般的なのですか?
アップデート2: もう一つの問題:原因私はちゃ外部のスレッドを実行しているという事実のために、パイプラインでOrderedMemoryAwareThreadPoolExecutor
を使用する方がよいでしょうか?パフォーマンスが向上しますか?
takeの代わりにpoll()を使用しないでください。 BlockingQueueの実装方法は? –
実際には、私がpoll()またはtake()を使用しているかどうかは関係ありません。ポーリングでは、タイムアウトを指定することができます。while(true)ループは必要ありません。take()はすぐにアイテムを取得します。ブロッキングキューについては、私の更新を参照してください。 –