Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HttpClient HttpRequestRetryHandler never invoked

I'm using the Apache HTTP commons DefaultHttpClient and after construction, I'm setting its retry handler:


httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
                @Override
                public boolean retryRequest(final IOException ioe,
                        final int numRetry, final HttpContext context)
                {
                    Log.d(TAG, "retry handler received exception of type: " + ioe.getClass().getName() + ", num retries: " + numRetry);
                    if (numRetry > 4) { // 3 retries
                        return false;
                    }
                    // Some exceptions we can retry without knowledge of which methods are being invoked
                    if (ioe instanceof NoHttpResponseException
                            || ioe instanceof UnknownHostException
                            || ioe instanceof SocketException) {
                    }
                    return false;
                }
        });

The server that I am sending my requests to frequently times out, and in the catch of my execute() call, I receive an IO error, "The operation timed out" however I never see any the "retry handler received exception of type" log statement in my console output. All of my requests are POST requests, but they are idempotent--they're safe to call multiple times without adverse side-effects.

Am I setting up the retry handler incorrectly? Is the retry handler only invoked in certain scenarios?

like image 670
skyler Avatar asked Nov 06 '22 05:11

skyler


1 Answers

Return true if you handled an exceptional case in your handler.

That should keep the exception from beeing thrown.

Also you could handle the Timeout exception in your handler as well.

httpClient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
                @Override
                public boolean retryRequest(final IOException ioe,
                        final int numRetry, final HttpContext context)
                {
                    Log.d(TAG, "retry handler received exception of type: " + ioe.getClass().getName() + ", num retries: " + numRetry);
                    if (numRetry > 4) { // 3 retries
                        return false;
                    }
                    // Some exceptions we can retry without knowledge of which methods are being invoked
                    if (ioe instanceof NoHttpResponseException
                            || ioe instanceof UnknownHostException
                            || ioe instanceof SocketException
                            // Replace with the actual type of the exception
                            || ioe instanceof TimeoutException) {
                                  return true;
                    }
                    return false;
                }
        });
like image 106
Ostkontentitan Avatar answered Nov 11 '22 02:11

Ostkontentitan