2012-03-28 58 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());