2012-03-28 56 views
2

每當我發現讀取器空閒時間時,我都會寫入超時錯誤。在讀取超時時寫入頻道

public class TimeOutHandler extends IdleStateAwareChannelHandler { 

     @Override 
     public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) { 
      if (e.getState() == IdleState.READER_IDLE) { 
       System.out.println("Reader TimeOut"); 
        HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); 
        response.setHeader(Names.CONTENT_TYPE, "application/json; charset=UTF-8"); 
        response.setContent(ChannelBuffers.copiedBuffer("{\"timeout\":true}", CharsetUtil.UTF_8)); 
        ChannelFuture future = e.getChannel().write(response); 
        future.addListener(ChannelFutureListener.CLOSE); 
      } 
     } 
} 

處理程序正在工作,但沒有任何信息寫入通道。這種情況可能嗎?

更新:

我管線廠:

public class AsyncServerPipelineFactory implements ChannelPipelineFactory { 

    static HashedWheelTimer timer = new HashedWheelTimer(); 

    private final ChannelHandler idleStateHandler = new IdleStateHandler(timer, 10, 20, 0); 
    public ChannelPipeline getPipeline() throws Exception { 
     ChannelPipeline pipeline = Channels.pipeline(idleStateHandler,new TimeOutHandler()); 
    pipeline.addLast("decoder", new HttpRequestDecoder()); 
    pipeline.addLast("encoder", new HttpResponseEncoder()); 
    pipeline.addLast("handler", new HTTPRequestHandler()); 
    return pipeline; 
    } 
} 
+0

你能告訴我們你的ChannelPipeline嗎? – 2012-03-29 11:54:15

+0

是的。請。看我的更新 – 2012-03-29 12:30:17

+0

看起來對我來說..爲什麼你認爲它沒有被寫入?你有沒有用wireshark捕捉流量? – 2012-03-30 05:34:41

回答

1

你的管道配置錯誤。寫入HttpResponse的任何處理程序必須在您的HttpResponseEncoder的之後插入。例如

ChannelPipeline pipeline = Channels.pipeline(); 
pipeline.addLast("idler", idleStateHandler); 
pipeline.addLast("decoder", new HttpRequestDecoder()); 
pipeline.addLast("encoder", new HttpResponseEncoder()); 
pipeline.addLast("timer-outer", new TimeOutHandler()); 
pipeline.addLast("handler", new HTTPRequestHandler());