use of com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler in project beam by apache.
the class HttpEventPublisher method execute.
/**
* Executes a POST for the list of {@link SplunkEvent} objects into Splunk's Http Event Collector
* endpoint.
*
* @param events list of {@link SplunkEvent}s
* @return {@link HttpResponse} for the POST
*/
HttpResponse execute(List<SplunkEvent> events) throws IOException {
HttpContent content = getContent(events);
HttpRequest request = requestFactory().buildPostRequest(genericUrl(), content);
HttpBackOffUnsuccessfulResponseHandler responseHandler = new HttpBackOffUnsuccessfulResponseHandler(getConfiguredBackOff());
responseHandler.setBackOffRequired(BackOffRequired.ON_SERVER_ERROR);
request.setUnsuccessfulResponseHandler(responseHandler);
setHeaders(request, token());
return request.execute();
}
use of com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler in project tutorials by eugenp.
the class GitHubExample method run.
// static final JsonFactory JSON_FACTORY = new GsonFactory();
private static void run() throws Exception {
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory((HttpRequest request) -> {
request.setParser(new JsonObjectParser(JSON_FACTORY));
});
GitHubUrl url = new GitHubUrl("https://api.github.com/users");
url.per_page = 10;
url.page = 1;
HttpRequest request = requestFactory.buildGetRequest(url);
ExponentialBackOff backoff = new ExponentialBackOff.Builder().setInitialIntervalMillis(500).setMaxElapsedTimeMillis(900000).setMaxIntervalMillis(6000).setMultiplier(1.5).setRandomizationFactor(0.5).build();
request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(backoff));
Type type = new TypeToken<List<User>>() {
}.getType();
List<User> users = (List<User>) request.execute().parseAs(type);
System.out.println(users);
url.appendRawPath("/eugenp");
request = requestFactory.buildGetRequest(url);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<HttpResponse> responseFuture = request.executeAsync(executor);
User eugen = responseFuture.get().parseAs(User.class);
System.out.println(eugen);
executor.shutdown();
}
use of com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler in project DataflowJavaSDK-examples by GoogleCloudPlatform.
the class RetryHttpInitializerWrapper method initialize.
/**
* Initializes the given request.
*/
@Override
public final void initialize(final HttpRequest request) {
// 2 minutes read timeout
request.setReadTimeout(2 * ONEMINITUES);
final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
request.setInterceptor(wrappedCredential);
request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {
@Override
public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean supportsRetry) throws IOException {
if (wrappedCredential.handleResponse(request, response, supportsRetry)) {
// and no backoff is desired.
return true;
} else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
// Otherwise, we defer to the judgement of
// our internal backoff handler.
LOG.info("Retrying " + request.getUrl().toString());
return true;
} else {
return false;
}
}
});
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
use of com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler in project kork by spinnaker.
the class GoogleUtils method setTimeoutsAndRetryBehavior.
static HttpRequestInitializer setTimeoutsAndRetryBehavior(final GoogleCredentials credentials) {
return new HttpCredentialsAdapter(credentials) {
public void initialize(HttpRequest request) throws IOException {
super.initialize(request);
request.setConnectTimeout(CONNECT_TIMEOUT);
request.setReadTimeout(READ_TIMEOUT);
HttpBackOffUnsuccessfulResponseHandler unsuccessfulResponseHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff());
unsuccessfulResponseHandler.setBackOffRequired(HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ON_SERVER_ERROR);
request.setUnsuccessfulResponseHandler(unsuccessfulResponseHandler);
}
};
}
Aggregations