Java Netty如何在HttpChunkAggregator上处理异常
我有一个应用程序,其中我使用HttpChunkAggregator来避免处理块,因为我需要将整个输入作为一个单元来解析,以创建Java Netty如何在HttpChunkAggregator上处理异常,java,netty,Java,Netty,我有一个应用程序,其中我使用HttpChunkAggregator来避免处理块,因为我需要将整个输入作为一个单元来解析,以创建json节点。由于HttpChunkAggregator必须采用maxContentLength,因此我需要处理传入请求超过内容大小的情况,因此我希望向客户端返回一条格式良好的错误消息。以下是我正在做的: 1:子类HttpChunkAggregator并重写exceptionCaught方法 public class MyHttpChunkAggregator exten
json
节点。由于HttpChunkAggregator
必须采用maxContentLength
,因此我需要处理传入请求超过内容大小的情况,因此我希望向客户端返回一条格式良好的错误消息。以下是我正在做的:
1:子类HttpChunkAggregator并重写exceptionCaught
方法
public class MyHttpChunkAggregator extends HttpChunkAggregator {
public MyHttpChunkAggregator(int maxContentLength) {
super(maxContentLength);
}
//@Override
public void exceptionCaught(ChannelHandlerContext context, ExceptionEvent ee)
throws Exception {
if (ee.getCause() instanceof TooLongFrameException) {
logger.log(Level.WARNING, "Exception caught in channel handler", ee.getCause());
HttpResponse httpResponse;
try {
//build a http response
httpResponse = //call my function here to build a response
ee.getChannel().write(httpResponse);
ee.getChannel().close();
} catch (IOException ioe) {
Throwables.propagate(ioe);
}
}
}
}
2:将自定义处理程序添加到管道
ChannelPipeline p = Channels.pipeline();
p.addLast("requestDecoder", new HttpRequestDecoder());
p.addLast("responseEncoder", new HttpResponseEncoder());
p.addLast("chunkAggregator", new MyHttpChunkAggregator(1048576)));
//adding the real business handle class to parse the input content
通过这样做,我能够实现messagereceived
在我真正的业务处理程序中不被调用,因为如果输入太大,我就不想再继续了。然而,我目前看到了两个我想要解决的问题:
exceptionCaught
被多次调用。我想在第一次发送一条格式良好的消息,然后永久终止对该请求的处理java.lang.IllegalStateException
:无法发送比请求更多的响应org.apache.http.NoHttpResponseException
:目标服务器未能响应谢谢,哪个版本?exceptionCaught(..)只能调用一次。我们在前一段时间修复了这个问题,所以也许你应该升级。我使用的是3.6.6.Final,哪个版本是修复程序?