use of org.apache.http.client.HttpRequestRetryHandler in project dropwizard by dropwizard.
the class HttpClientBuilderTest method usesACustomHttpRequestRetryHandler.
@Test
public void usesACustomHttpRequestRetryHandler() throws Exception {
final HttpRequestRetryHandler customHandler = (exception, executionCount, context) -> false;
configuration.setRetries(1);
assertThat(builder.using(configuration).using(customHandler).createClient(apacheBuilder, connectionManager, "test")).isNotNull();
assertThat(spyHttpClientBuilderField("retryHandler", apacheBuilder)).isSameAs(customHandler);
}
use of org.apache.http.client.HttpRequestRetryHandler in project Libraries-for-Android-Developers by eoecn.
the class AsyncHttpRequest method makeRequestWithRetries.
private void makeRequestWithRetries() throws IOException {
boolean retry = true;
IOException cause = null;
HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
try {
while (retry) {
try {
makeRequest();
return;
} catch (UnknownHostException e) {
// switching between WI-FI and mobile data networks can cause a retry which then results in an UnknownHostException
// while the WI-FI is initialising. The retry logic will be invoked here, if this is NOT the first retry
// (to assist in genuine cases of unknown host) which seems better than outright failure
cause = new IOException("UnknownHostException exception: " + e.getMessage());
retry = (executionCount > 0) && retryHandler.retryRequest(cause, ++executionCount, context);
} catch (NullPointerException e) {
// there's a bug in HttpClient 4.0.x that on some occasions causes
// DefaultRequestExecutor to throw an NPE, see
// http://code.google.com/p/android/issues/detail?id=5255
cause = new IOException("NPE in HttpClient: " + e.getMessage());
retry = retryHandler.retryRequest(cause, ++executionCount, context);
} catch (IOException e) {
if (isCancelled()) {
// Eating exception, as the request was cancelled
return;
}
cause = e;
retry = retryHandler.retryRequest(cause, ++executionCount, context);
}
if (retry && (responseHandler != null)) {
responseHandler.sendRetryMessage(executionCount);
}
}
} catch (Exception e) {
// catch anything else to ensure failure message is propagated
Log.e("AsyncHttpRequest", "Unhandled exception origin cause", e);
cause = new IOException("Unhandled exception: " + e.getMessage());
}
// cleaned up to throw IOException
throw (cause);
}
use of org.apache.http.client.HttpRequestRetryHandler in project openkit-android by OpenKit.
the class AsyncHttpRequest method makeRequestWithRetries.
private void makeRequestWithRetries() throws ConnectException {
// This is an additional layer of retry logic lifted from droid-fu
// See: https://github.com/kaeppler/droid-fu/blob/master/src/main/java/com/github/droidfu/http/BetterHttpRequestBase.java
boolean retry = true;
IOException cause = null;
HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
while (retry) {
try {
makeRequest();
return;
} catch (UnknownHostException e) {
if (responseHandler != null) {
responseHandler.sendFailureMessage(e, "can't resolve host");
}
return;
} catch (SocketException e) {
// Added to detect host unreachable
if (responseHandler != null) {
responseHandler.sendFailureMessage(e, "can't resolve host");
}
return;
} catch (SocketTimeoutException e) {
if (responseHandler != null) {
responseHandler.sendFailureMessage(e, "socket time out");
}
return;
} catch (IOException e) {
cause = e;
retry = retryHandler.retryRequest(cause, ++executionCount, context);
} catch (NullPointerException e) {
// there's a bug in HttpClient 4.0.x that on some occasions causes
// DefaultRequestExecutor to throw an NPE, see
// http://code.google.com/p/android/issues/detail?id=5255
cause = new IOException("NPE in HttpClient" + e.getMessage());
retry = retryHandler.retryRequest(cause, ++executionCount, context);
}
}
// no retries left, crap out with exception
ConnectException ex = new ConnectException();
ex.initCause(cause);
throw ex;
}
use of org.apache.http.client.HttpRequestRetryHandler in project jersey by jersey.
the class RetryHandlerTest method testRetryGet.
@Test
public void testRetryGet() throws IOException {
ClientConfig cc = new ClientConfig();
cc.connectorProvider(new ApacheConnectorProvider());
cc.property(ApacheClientProperties.RETRY_HANDLER, (HttpRequestRetryHandler) (exception, executionCount, context) -> true);
cc.property(ClientProperties.READ_TIMEOUT, READ_TIMEOUT_MS);
Client client = ClientBuilder.newClient(cc);
WebTarget r = client.target(getBaseUri());
assertEquals("GET", r.request().get(String.class));
}
use of org.apache.http.client.HttpRequestRetryHandler in project afinal by yangfuhai.
the class SyncRequestHandler method makeRequestWithRetries.
private Object makeRequestWithRetries(HttpUriRequest request) throws IOException {
boolean retry = true;
IOException cause = null;
HttpRequestRetryHandler retryHandler = client.getHttpRequestRetryHandler();
while (retry) {
try {
HttpResponse response = client.execute(request, context);
return entityHandler.handleEntity(response.getEntity(), null, charset);
} catch (UnknownHostException e) {
cause = e;
retry = retryHandler.retryRequest(cause, ++executionCount, context);
} catch (IOException e) {
cause = e;
retry = retryHandler.retryRequest(cause, ++executionCount, context);
} catch (NullPointerException e) {
// HttpClient 4.0.x 之前的一个bug
// http://code.google.com/p/android/issues/detail?id=5255
cause = new IOException("NPE in HttpClient" + e.getMessage());
retry = retryHandler.retryRequest(cause, ++executionCount, context);
} catch (Exception e) {
cause = new IOException("Exception" + e.getMessage());
retry = retryHandler.retryRequest(cause, ++executionCount, context);
}
}
if (cause != null)
throw cause;
else
throw new IOException("未知网络错误");
}
Aggregations