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

nl.topicus.jdbc.shaded.com.google.cloud.RetryHelper Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
/*
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in nl.topicus.jdbc.shaded.com.liance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.nl.topicus.jdbc.shaded.org.licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.topicus.jdbc.shaded.com.google.cloud;

import nl.topicus.jdbc.shaded.com.google.api.core.ApiClock;
import nl.topicus.jdbc.shaded.com.google.api.core.BetaApi;
import nl.topicus.jdbc.shaded.com.google.api.gax.retrying.ResultRetryAlgorithm;
import nl.topicus.jdbc.shaded.com.google.api.gax.retrying.RetrySettings;

import nl.topicus.jdbc.shaded.com.google.api.gax.retrying.DirectRetryingExecutor;
import nl.topicus.jdbc.shaded.com.google.api.gax.retrying.ExponentialRetryAlgorithm;
import nl.topicus.jdbc.shaded.com.google.api.gax.retrying.RetryAlgorithm;
import nl.topicus.jdbc.shaded.com.google.api.gax.retrying.RetryingExecutor;
import nl.topicus.jdbc.shaded.com.google.api.gax.retrying.RetryingFuture;
import java.util.concurrent.Callable;

/**
 * Utility class for retrying operations. For more details about the parameters, see {@link
 * RetrySettings}. In case if retrying is unsuccessful, {@link RetryHelperException} will be
 * thrown.
 */
@BetaApi
public class RetryHelper {
  public static  V runWithRetries(
      Callable callable,
      RetrySettings retrySettings,
      ResultRetryAlgorithm resultRetryAlgorithm,
      ApiClock clock)
      throws RetryHelperException {
    try {
      // Suppressing should be ok as a workaraund. Current and only ResultRetryAlgorithm
      // implementation does not use response at all, so ignoring its type is ok.
      @SuppressWarnings("unchecked")
      RetryAlgorithm retryAlgorithm =
          new RetryAlgorithm<>((ResultRetryAlgorithm) resultRetryAlgorithm,
              new ExponentialRetryAlgorithm(retrySettings, clock));
      RetryingExecutor executor = new DirectRetryingExecutor<>(retryAlgorithm);

      RetryingFuture retryingFuture = executor.createFuture(callable);
      executor.submit(retryingFuture);

      return retryingFuture.get();
    } catch (Exception e) {
      throw new RetryHelperException(e.getCause());
    }
  }

  public static class RetryHelperException extends RuntimeException {

    private static final long serialVersionUID = -8519852520090965314L;

    RetryHelperException(Throwable cause) {
      super(cause);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy