每当我抓住Reader Idle超时时,我想写出超时错误。
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 :(得分:1)
您的管道配置错误。必须在 HttpResponseEncoder之后插入任何写入HttpResponse的处理程序。例如
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());