Search in sources :

Example 11 with HttpBackOffUnsuccessfulResponseHandler

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();
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) HttpContent(com.google.api.client.http.HttpContent)

Example 12 with HttpBackOffUnsuccessfulResponseHandler

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();
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) HttpResponse(com.google.api.client.http.HttpResponse) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff) Type(java.lang.reflect.Type) JsonObjectParser(com.google.api.client.json.JsonObjectParser) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List)

Example 13 with HttpBackOffUnsuccessfulResponseHandler

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));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpBackOffIOExceptionHandler(com.google.api.client.http.HttpBackOffIOExceptionHandler) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff)

Example 14 with HttpBackOffUnsuccessfulResponseHandler

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);
        }
    };
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpCredentialsAdapter(com.google.auth.http.HttpCredentialsAdapter) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff)

Aggregations

HttpBackOffUnsuccessfulResponseHandler (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler)14 HttpRequest (com.google.api.client.http.HttpRequest)13 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)13 HttpResponse (com.google.api.client.http.HttpResponse)10 HttpBackOffIOExceptionHandler (com.google.api.client.http.HttpBackOffIOExceptionHandler)9 IOException (java.io.IOException)7 HttpUnsuccessfulResponseHandler (com.google.api.client.http.HttpUnsuccessfulResponseHandler)6 GenericUrl (com.google.api.client.http.GenericUrl)5 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)4 JsonObjectParser (com.google.api.client.json.JsonObjectParser)4 HttpHeaders (com.google.api.client.http.HttpHeaders)2 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)2 JwtBuilder (io.jsonwebtoken.JwtBuilder)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Credential (com.google.api.client.auth.oauth2.Credential)1 ByteArrayContent (com.google.api.client.http.ByteArrayContent)1 BackOffRequired (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired)1 HttpContent (com.google.api.client.http.HttpContent)1 HttpTransport (com.google.api.client.http.HttpTransport)1