Search in sources :

Example 1 with BackOffPolicy

use of com.google.api.client.http.BackOffPolicy in project google-api-java-client by google.

the class BatchUnparsedResponse method parseAndCallback.

/**
 * Parse an object into a new instance of the data class using
 * {@link HttpResponse#parseAs(java.lang.reflect.Type)}.
 */
private <T, E> void parseAndCallback(RequestInfo<T, E> requestInfo, int statusCode, HttpResponse response) throws IOException {
    BatchCallback<T, E> callback = requestInfo.callback;
    HttpHeaders responseHeaders = response.getHeaders();
    HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler = requestInfo.request.getUnsuccessfulResponseHandler();
    BackOffPolicy backOffPolicy = requestInfo.request.getBackOffPolicy();
    // Reset backOff flag.
    backOffRequired = false;
    if (HttpStatusCodes.isSuccess(statusCode)) {
        if (callback == null) {
            // No point in parsing if there is no callback.
            return;
        }
        T parsed = getParsedDataClass(requestInfo.dataClass, response, requestInfo);
        callback.onSuccess(parsed, responseHeaders);
    } else {
        HttpContent content = requestInfo.request.getContent();
        boolean retrySupported = retryAllowed && (content == null || content.retrySupported());
        boolean errorHandled = false;
        boolean redirectRequest = false;
        if (unsuccessfulResponseHandler != null) {
            errorHandled = unsuccessfulResponseHandler.handleResponse(requestInfo.request, response, retrySupported);
        }
        if (!errorHandled) {
            if (requestInfo.request.handleRedirect(response.getStatusCode(), response.getHeaders())) {
                redirectRequest = true;
            } else if (retrySupported && backOffPolicy != null && backOffPolicy.isBackOffRequired(response.getStatusCode())) {
                backOffRequired = true;
            }
        }
        if (retrySupported && (errorHandled || backOffRequired || redirectRequest)) {
            unsuccessfulRequestInfos.add(requestInfo);
        } else {
            if (callback == null) {
                // No point in parsing if there is no callback.
                return;
            }
            E parsed = getParsedDataClass(requestInfo.errorClass, response, requestInfo);
            callback.onFailure(parsed, responseHeaders);
        }
    }
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) BackOffPolicy(com.google.api.client.http.BackOffPolicy) HttpUnsuccessfulResponseHandler(com.google.api.client.http.HttpUnsuccessfulResponseHandler) HttpContent(com.google.api.client.http.HttpContent)

Example 2 with BackOffPolicy

use of com.google.api.client.http.BackOffPolicy in project google-api-java-client by google.

the class BatchRequest method execute.

/**
 * Executes all queued HTTP requests in a single call, parses the responses and invokes callbacks.
 *
 * <p>
 * Calling {@link #execute()} executes and clears the queued requests. This means that the
 * {@link BatchRequest} object can be reused to {@link #queue} and {@link #execute()} requests
 * again.
 * </p>
 */
public void execute() throws IOException {
    boolean retryAllowed;
    Preconditions.checkState(!requestInfos.isEmpty());
    HttpRequest batchRequest = requestFactory.buildPostRequest(this.batchUrl, null);
    // NOTE: batch does not support gzip encoding
    HttpExecuteInterceptor originalInterceptor = batchRequest.getInterceptor();
    batchRequest.setInterceptor(new BatchInterceptor(originalInterceptor));
    int retriesRemaining = batchRequest.getNumberOfRetries();
    BackOffPolicy backOffPolicy = batchRequest.getBackOffPolicy();
    if (backOffPolicy != null) {
        // Reset the BackOffPolicy at the start of each execute.
        backOffPolicy.reset();
    }
    do {
        retryAllowed = retriesRemaining > 0;
        MultipartContent batchContent = new MultipartContent();
        batchContent.getMediaType().setSubType("mixed");
        int contentId = 1;
        for (RequestInfo<?, ?> requestInfo : requestInfos) {
            batchContent.addPart(new MultipartContent.Part(new HttpHeaders().setAcceptEncoding(null).set("Content-ID", contentId++), new HttpRequestContent(requestInfo.request)));
        }
        batchRequest.setContent(batchContent);
        HttpResponse response = batchRequest.execute();
        BatchUnparsedResponse batchResponse;
        try {
            // Find the boundary from the Content-Type header.
            String boundary = "--" + response.getMediaType().getParameter("boundary");
            // Parse the content stream.
            InputStream contentStream = response.getContent();
            batchResponse = new BatchUnparsedResponse(contentStream, boundary, requestInfos, retryAllowed);
            while (batchResponse.hasNext) {
                batchResponse.parseNextResponse();
            }
        } finally {
            response.disconnect();
        }
        List<RequestInfo<?, ?>> unsuccessfulRequestInfos = batchResponse.unsuccessfulRequestInfos;
        if (!unsuccessfulRequestInfos.isEmpty()) {
            requestInfos = unsuccessfulRequestInfos;
            // backOff if required.
            if (batchResponse.backOffRequired && backOffPolicy != null) {
                long backOffTime = backOffPolicy.getNextBackOffMillis();
                if (backOffTime != BackOffPolicy.STOP) {
                    try {
                        sleeper.sleep(backOffTime);
                    } catch (InterruptedException exception) {
                    // ignore
                    }
                }
            }
        } else {
            break;
        }
        retriesRemaining--;
    } while (retryAllowed);
    requestInfos.clear();
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) BackOffPolicy(com.google.api.client.http.BackOffPolicy) InputStream(java.io.InputStream) HttpResponse(com.google.api.client.http.HttpResponse) MultipartContent(com.google.api.client.http.MultipartContent) HttpExecuteInterceptor(com.google.api.client.http.HttpExecuteInterceptor)

Aggregations

BackOffPolicy (com.google.api.client.http.BackOffPolicy)2 HttpHeaders (com.google.api.client.http.HttpHeaders)2 HttpContent (com.google.api.client.http.HttpContent)1 HttpExecuteInterceptor (com.google.api.client.http.HttpExecuteInterceptor)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpResponse (com.google.api.client.http.HttpResponse)1 HttpUnsuccessfulResponseHandler (com.google.api.client.http.HttpUnsuccessfulResponseHandler)1 MultipartContent (com.google.api.client.http.MultipartContent)1 InputStream (java.io.InputStream)1