下面列出了怎么用com.google.api.client.http.HttpBackOffIOExceptionHandler的API类实例代码及写法,或者点击链接到github查看源代码。
/** Initializes the given request. */
@Override
public final void initialize(final HttpRequest request) {
request.setReadTimeout(2 * ONEMINITUES); // 2 minutes read timeout
final HttpUnsuccessfulResponseHandler backoffHandler =
new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
request.setInterceptor(wrappedCredential);
request.setUnsuccessfulResponseHandler(
(request1, response, supportsRetry) -> {
if (wrappedCredential.handleResponse(request1, response, supportsRetry)) {
// If credential decides it can handle it, the return code or message indicated
// something specific to authentication, and no backoff is desired.
return true;
} else if (backoffHandler.handleResponse(request1, response, supportsRetry)) {
// Otherwise, we defer to the judgement of our internal backoff handler.
LOG.info("Retrying " + request1.getUrl().toString());
return true;
} else {
return false;
}
});
request.setIOExceptionHandler(
new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
@Test
public void testRetryConfigWithIOExceptionHandling() throws Exception {
FirebaseApp app = FirebaseApp.initializeApp(new FirebaseOptions.Builder()
.setCredentials(new MockGoogleCredentials("token"))
.build());
RetryConfig retryConfig = RetryConfig.builder()
.setMaxRetries(MAX_RETRIES)
.setRetryOnIOExceptions(true)
.build();
HttpRequest request = TestUtils.createRequest();
FirebaseRequestInitializer initializer = new FirebaseRequestInitializer(app, retryConfig);
initializer.initialize(request);
assertEquals(0, request.getConnectTimeout());
assertEquals(0, request.getReadTimeout());
assertEquals("Bearer token", request.getHeaders().getAuthorization());
assertEquals(MAX_RETRIES, request.getNumberOfRetries());
assertTrue(request.getIOExceptionHandler() instanceof HttpBackOffIOExceptionHandler);
assertNotNull(request.getUnsuccessfulResponseHandler());
}
/** Initializes the given request. */
@Override
public final void initialize(final HttpRequest request) {
request.setReadTimeout(2 * ONEMINITUES); // 2 minutes read timeout
final HttpUnsuccessfulResponseHandler backoffHandler =
new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
request.setInterceptor(wrappedCredential);
request.setUnsuccessfulResponseHandler(
(request1, response, supportsRetry) -> {
if (wrappedCredential.handleResponse(request1, response, supportsRetry)) {
// If credential decides it can handle it, the return code or message indicated
// something specific to authentication, and no backoff is desired.
return true;
} else if (backoffHandler.handleResponse(request1, response, supportsRetry)) {
// Otherwise, we defer to the judgement of our internal backoff handler.
LOG.info("Retrying " + request1.getUrl().toString());
return true;
} else {
return false;
}
});
request.setIOExceptionHandler(
new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
@Test
public void testEnableRetry() throws IOException {
RetryConfig retryConfig = retryOnIOAndServiceUnavailableErrors(new MockSleeper());
RetryInitializer initializer = new RetryInitializer(retryConfig);
HttpRequest request = TestUtils.createRequest();
initializer.initialize(request);
assertEquals(MAX_RETRIES, request.getNumberOfRetries());
assertTrue(request.getUnsuccessfulResponseHandler() instanceof RetryHandlerDecorator);
RetryUnsuccessfulResponseHandler retryHandler =
((RetryHandlerDecorator) request.getUnsuccessfulResponseHandler()).getRetryHandler();
assertSame(retryConfig, retryHandler.getRetryConfig());
assertTrue(request.getIOExceptionHandler() instanceof HttpBackOffIOExceptionHandler);
}
private HttpRequestFactory createRequestFactory() {
return new NetHttpTransport().createRequestFactory(request -> {
request.setConnectTimeout(HttpClient.this.config.getConnectTimeout());
request.setReadTimeout(HttpClient.this.config.getReadTimeout());
request.setNumberOfRetries(HttpClient.this.config.getRetries());
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff.Builder().build()));
});
}
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
if (credential != null) {
httpRequest.setInterceptor(credential);
}
httpRequest.setIOExceptionHandler(
new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
httpRequest.setUnsuccessfulResponseHandler(
new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()));
}
public LoggingIOExceptionHandler(BackOff backOff) {
delegate = new HttpBackOffIOExceptionHandler(backOff);
}
private HttpIOExceptionHandler newIOExceptionHandler() {
return new HttpBackOffIOExceptionHandler(retryConfig.newBackOff())
.setSleeper(retryConfig.getSleeper());
}
private HttpIOExceptionHandler buildIOExceptionHandler() {
return new HttpBackOffIOExceptionHandler(getBackOff());
}
@Override
public void initialize(HttpRequest request) {
// Credential must be the interceptor to fill in accessToken fields.
request.setInterceptor(credential);
// Request will be retried if server errors (5XX) or I/O errors are encountered.
request.setNumberOfRetries(options.getMaxRequestRetries());
// Set the timeout configurations.
request.setConnectTimeout(Math.toIntExact(options.getConnectTimeout().toMillis()));
request.setReadTimeout(Math.toIntExact(options.getReadTimeout().toMillis()));
// IOExceptions such as "socket timed out" of "insufficient bytes written" will follow a
// straightforward backoff.
HttpBackOffIOExceptionHandler exceptionHandler =
new HttpBackOffIOExceptionHandler(new ExponentialBackOff());
if (sleeperOverride != null) {
exceptionHandler.setSleeper(sleeperOverride);
}
// Supply a new composite handler for unsuccessful return codes. 401 Unauthorized will be
// handled by the Credential, 410 Gone will be logged, and 5XX will be handled by a backoff
// handler.
LoggingResponseHandler loggingResponseHandler =
new LoggingResponseHandler(
new CredentialOrBackoffResponseHandler(),
exceptionHandler,
ImmutableSet.of(HttpStatus.SC_GONE, HttpStatus.SC_SERVICE_UNAVAILABLE),
ImmutableSet.of(HTTP_SC_TOO_MANY_REQUESTS));
request.setUnsuccessfulResponseHandler(loggingResponseHandler);
request.setIOExceptionHandler(loggingResponseHandler);
if (isNullOrEmpty(request.getHeaders().getUserAgent())
&& !isNullOrEmpty(options.getDefaultUserAgent())) {
logger.atFiner().log(
"Request is missing a user-agent, adding default value of '%s'",
options.getDefaultUserAgent());
request.getHeaders().setUserAgent(options.getDefaultUserAgent());
}
request.getHeaders().putAll(options.getHttpHeaders());
}
public void initialize(HttpRequest request) {
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(BackOff.ZERO_BACKOFF));
request.setUnsuccessfulResponseHandler(
new HttpBackOffUnsuccessfulResponseHandler(BackOff.ZERO_BACKOFF));
}