All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.bingads.internal.OperationStatusRetry Maven / Gradle / Ivy

Go to download

The Bing Ads Java SDK is a library improving developer experience when working with the Bing Ads services by providing high-level access to features such as Bulk API, OAuth Authorization and SOAP API.

There is a newer version: 13.0.22.1
Show newest version
package com.microsoft.bingads.internal;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import com.microsoft.bingads.AsyncCallback;
import com.microsoft.bingads.ServiceClient;
import com.microsoft.bingads.internal.functionalinterfaces.Consumer;
import com.microsoft.bingads.internal.functionalinterfaces.TriConsumer;

public class OperationStatusRetry {
    private static final int INTERVAL_OF_RETRY = 1000; // TimeUnit Milliseconds

    private static final int[] INTERVAL_OF_RETRY_RATE_LIMIT = { 15000, 20000, 25000, 30000 }; // TimeUnit

    private static final int RATELIMIT_CODE = 117;

    private ScheduledExecutorService executor;

    private Function errorCodeFunc;

    public OperationStatusRetry(Function f) {
        errorCodeFunc = f;
    }

    public void executeWithRetry(
            final TriConsumer, AsyncCallback> action,
            final TOperationStatusProvider statusProvider, final ServiceClient serviceClient,
            final Consumer statusConsumer, final Consumer exceptionConsumer,
            final int maxRetryCount) {
        executor = Executors.newSingleThreadScheduledExecutor();
        doPollOperationStatus(action, statusProvider, serviceClient, statusConsumer, exceptionConsumer, maxRetryCount);
    }

    private void doPollOperationStatus(
            final TriConsumer, AsyncCallback> action,
            final TOperationStatusProvider statusProvider, final ServiceClient serviceClient,
            final Consumer statusConsumer, final Consumer exceptionConsumer,
            final int maxRetryCount) {
        action.accept(statusProvider, serviceClient, new AsyncCallback() {

            @Override
            public void onCompleted(Future result) {
                try {
                    statusConsumer.accept(result.get());
                    executor.shutdown();
                } catch (InterruptedException exception) {
                    retryWhenException(action, statusProvider, serviceClient, statusConsumer, exceptionConsumer,
                            maxRetryCount, exception);
                } catch (ExecutionException exception) {
                    retryWhenException(action, statusProvider, serviceClient, statusConsumer, exceptionConsumer,
                            maxRetryCount, exception);
                }
            }

            private void retryWhenException(
                    final TriConsumer, AsyncCallback> action,
                    final TOperationStatusProvider statusProvider, final ServiceClient serviceClient,
                    final Consumer statusConsumer, final Consumer exceptionConsumer,
                    final int maxRetryCount, Exception exception) {
                if (maxRetryCount > 0) {
                    retry(action, statusProvider, serviceClient, statusConsumer, exceptionConsumer, maxRetryCount - 1,
                            exception);
                } else {
                    executor.shutdown();
                    exceptionConsumer.accept(exception);
                }
            }
        });
    }

    private void retry(
            final TriConsumer, AsyncCallback> action,
            final TOperationStatusProvider statusProvider, final ServiceClient serviceClient,
            final Consumer statusConsumer, final Consumer exceptionConsumer,
            final int maxRetryCount, Exception exception) {
        int interval = getInterval(exception, maxRetryCount);
        executor.schedule(new Runnable() {
            @Override
            public void run() {
                doPollOperationStatus(action, statusProvider, serviceClient, statusConsumer, exceptionConsumer,
                        maxRetryCount);
            }
        }, interval, TimeUnit.MILLISECONDS);

    }

    private int getInterval(Exception exception, int maxRetryCount) {
        int errorCode  = errorCodeFunc.apply(exception);
        if (errorCode == RATELIMIT_CODE) {
            return INTERVAL_OF_RETRY_RATE_LIMIT[INTERVAL_OF_RETRY_RATE_LIMIT.length - 1 - maxRetryCount];
        } else {
            return INTERVAL_OF_RETRY;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy