java Netty 异常处理——Handler 抛出异常,然后呢?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9856077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Netty exception handling - Handler throws Exception, then what?
提问by Matt Friedman
I've searching around for an exception handling pattern for Netty but I'm not able to find much.
我一直在寻找 Netty 的异常处理模式,但找不到太多。
Some sort of exception handling guide would be great. I have exceptions thrown that are sent to exceptionCaught but I don't know what to do next.
某种异常处理指南会很棒。我抛出了发送到 exceptionCaught 的异常,但我不知道下一步该怎么做。
Can someone provide a general purpose explanation of how to handle exceptions in Netty. What is the expected pattern for handling an exception thrown from a ChannelHandler?
有人可以提供有关如何在 Netty 中处理异常的通用解释。处理从 ChannelHandler 抛出的异常的预期模式是什么?
Thanks, Matt
谢谢,马特
回答by James K
As Norman and Veebs have both mentioned, without understanding your precise requirements it's a little tricky to give a precise answer however.... I think the following provides a generic way to handle server errors that you were not expecting. It returns an HTTP 500 'Internal Server Error' to the client and then closes the channel. Obviously I'm making the assumption that your clients are requesting and receiving over HTTP which they may not be, in which case Veebs's solution is better.
正如 Norman 和 Veebs 都提到的那样,在不了解您的确切要求的情况下,给出准确的答案有点棘手,但是……我认为以下内容提供了一种处理您意想不到的服务器错误的通用方法。它向客户端返回 HTTP 500“内部服务器错误”,然后关闭通道。显然,我假设您的客户正在通过 HTTP 请求和接收它们可能不是,在这种情况下 Veebs 的解决方案更好。
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
public class ServerErrorHandler extends SimpleChannelHandler {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
HttpResponse err = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
HttpResponseStatus.INTERNAL_SERVER_ERROR);
e.getChannel().write(err).addListener(ChannelFutureListener.CLOSE);
}
}
Note if you use this solution then you will need to add an HttpResponseDecoder to your pipeline also.
请注意,如果您使用此解决方案,那么您还需要将 HttpResponseDecoder 添加到您的管道中。
Obviously if you have specific exceptions that you wish to catch and handle then you'd write some additional logic here to do that.
显然,如果您有希望捕获和处理的特定异常,那么您可以在此处编写一些额外的逻辑来做到这一点。
HTH!
哼!
回答by Norman Maurer
It really depends on your implementation and what type of Exception. Sometimes you may be able to recover, other times it may be the best to just close the Channel.
这真的取决于你的实现和什么类型的异常。有时您可能能够恢复,有时最好关闭通道。
So I think its impossible to tell you how to handle it..
所以我认为不可能告诉你如何处理它..
回答by Veebs
Agree with Norman.
同意诺曼。
In general, I try to catch and handle all application exception and return proper messages containing the errors.
通常,我尝试捕获和处理所有应用程序异常并返回包含错误的正确消息。
For example, in a HTTP server, I would return a 404 if a file was not found.
例如,在 HTTP 服务器中,如果找不到文件,我将返回 404。
I also add the following function in my handler for any exceptions I did not catch - which in theory should only be network type errors. I tend to take a black and white approach to these exceptions and assume that I cannot recover. Hence, I close the channel. It will be up to the client to try again.
我还在我的处理程序中为我没有捕捉到的任何异常添加了以下函数——理论上应该只是网络类型错误。我倾向于对这些异常采取非黑即白的方法,并假设我无法恢复。因此,我关闭了频道。由客户再试一次。
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
try {
_logger.error(e.getCause(), "ERROR: Unhandled exception: " + e.getCause().getMessage()
+ ". Closing channel " + ctx.getChannel().getId());
e.getChannel().close();
} catch (Exception ex) {
_logger.debug(ex, "ERROR trying to close socket because we got an unhandled exception");
}
}
Hope this helps.
希望这可以帮助。