简体   繁体   English

HttpRequestRetryHandler处理哪些异常?

[英]What exceptions are handled by HttpRequestRetryHandler?

I am trying to implement HttpRequestRetryHandler in my Android app. 我正在尝试在我的Android应用中实现HttpRequestRetryHandler。 I have read the documentation for HttpRequestRetryHandler here . 在这里阅读了HttpRequestRetryHandler的文档。

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {

    public boolean retryRequest(
            IOException exception, 
            int executionCount,
            HttpContext context) {
        if (executionCount >= 5) {
            // Do not retry if over max retry count
            return false;
        }
        if (exception instanceof InterruptedIOException) {
            // Timeout
            return false;
        }
        if (exception instanceof UnknownHostException) {
            // Unknown host
            return false;
        }
        if (exception instanceof ConnectException) {
            // Connection refused
            return false;
        }
        if (exception instanceof SSLException) {
            // SSL handshake exception
            return false;
        }
        HttpRequest request = (HttpRequest) context.getAttribute(
                ExecutionContext.HTTP_REQUEST);
        boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); 
        if (idempotent) {
            // Retry if the request is considered idempotent 
            return true;
        }
        return false;
    }

};

httpclient.setHttpRequestRetryHandler(myRetryHandler);

The example in documentation states that certain exceptions should not be retried. 文档中的示例指出,某些异常不应重试。 For example InterruptedIOException should not be retried. 例如,不应重试InterruptedIOException。

Question 1 Why InterruptedIOException should not be retried? 问题1为什么不应该重试InterruptedIOException?

Question 2 How to know which exception to retry and which should not? 问题2如何知道重试哪个异常,不应该重试? For example - Should we retry ConnectionTimeoutException and SocketTimeoutException or not? 例如-我们是否应该重试ConnectionTimeoutException和SocketTimeoutException?

Also documentation says HttpClient assumes non-entity enclosing methods such as GET and HEAD to be idempotent and entity enclosing methods such as POST and PUT to be not. 另外,文档还说HttpClient假定非实体封装方法(例如GET和HEAD)是幂等的,而实体封装方法(例如POST和PUT)则不是幂等的

Question 3 Does this mean we should not retry POST and PUT method and if not, then how should we retry HttpPost request? 问题3这是否意味着我们不应该重试POST和PUT方法,如果不是,那么我们应该如何重试HttpPost请求?

I think its just an example of RetryHandler implementation. 我认为这只是RetryHandler实现的一个示例。

Question 1 Why InterruptedIOException should not be retried? 问题1为什么不应该重试InterruptedIOException?

In the example, I believe it's assumed that cause of timeout is resource unavailability at URL and hence retry will not do anything better. 在该示例中,我认为已假定超时的原因是resource unavailability at URL上的resource unavailability at URL因此重试不会做得更好。 That's the reason, it's set to false. 这就是原因,它设置为false。 If in your case, you think it may be just because of slow connection or some intermittent issue, feel free to set it true for retry. 如果您认为是由于连接缓慢或某些间歇性问题引起的,请随时将其设置为重试。

Question 2 How to know which exception to retry and which should not? 问题2如何知道重试哪个异常,不应该重试? For example - Should we retry ConnectionTimeoutException and SocketTimeoutException or not? 例如-我们是否应该重试ConnectionTimeoutException和SocketTimeoutException?

Any exception, which you believe that it's because of transient issue, you may retry. 您认为是由于暂时性问题而导致的任何异常,都可以重试。 Any exception whose cause is more of permanent in the nature eg Resource Not Found or some business exception shouldn't be retried as they will always fail. 本质上具有永久原因的任何异常(例如,找不到资源或某些业务异常)都不应重试,因为它们总是会失败。

Question 3 Does this mean we should not retry POST and PUT method and if not, then how should we retry HttpPost request? 问题3这是否意味着我们不应该重试POST和PUT方法,如果不是,那么我们应该如何重试HttpPost请求?

Retry is permitted only for the scenarios where you can re-initiate the process without any external dependency. 仅在可以重新启动该过程而没有任何外部依赖性的情况下才允许重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM