HttpRequestInitializer is required to perform automatic retry upon failures. A simple HttpRequestInitializer can be constructed using com.google.api.client.auth.oauth2.Credential as follows:
private HttpRequestInitializer buildInitializer(Credential credential) {
return httpRequest -> {
// handles abnormal HTTP responses (non 2XX responses)
final HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler =
new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())
.setSleeper(Sleeper.DEFAULT);
/*
this is designed to work with only one HttpRequest at a time.
Hence, a new instance of HttpBackOffIOExceptionHandler with new instance of
BackOff is created for each instance of HttpRequest
*/
HttpBackOffIOExceptionHandler httpBackOffIOExceptionHandler =
new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(Sleeper.DEFAULT);
httpRequest.setInterceptor(credential).setUnsuccessfulResponseHandler(unsuccessfulResponseHandler)
.setIOExceptionHandler(httpBackOffIOExceptionHandler)
.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(3))
.setReadTimeout((int) TimeUnit.MINUTES.toMillis(3))
.setThrowExceptionOnExecuteError(false).setSuppressUserAgentSuffix(true);
};
}
HttpRequestInitializeris made mandatory for a reason. What is stopping you from creating aHttpRequestInitializer? - Shyam Baitmangalkar