com.google.common.util.concurrent.AsyncCallable Java Examples

The following examples show how to use com.google.common.util.concurrent.AsyncCallable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Retrier.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
/**
 * Executes an {@link AsyncCallable}, retrying execution in case of failure with the given
 * backoff.
 */
public <T> ListenableFuture<T> executeAsync(AsyncCallable<T> call, Backoff backoff) {
  try {
    return Futures.catchingAsync(
        call.call(),
        Exception.class,
        t -> onExecuteAsyncFailure(t, call, backoff),
        MoreExecutors.directExecutor());
  } catch (Exception e) {
    return onExecuteAsyncFailure(e, call, backoff);
  }
}
 
Example #2
Source File: Retrier.java    From bazel-buildfarm with Apache License 2.0 5 votes vote down vote up
private <T> ListenableFuture<T> onExecuteAsyncFailure(
    Exception t, AsyncCallable<T> call, Backoff backoff) {
  long waitMillis = backoff.nextDelayMillis();
  if (waitMillis >= 0 && isRetriable.apply(Status.fromThrowable(t))) {
    try {
      return Futures.scheduleAsync(
          () -> executeAsync(call, backoff), waitMillis, TimeUnit.MILLISECONDS, retryScheduler);
    } catch (RejectedExecutionException e) {
      // May be thrown by .scheduleAsync(...) if i.e. the executor is shutdown.
      return Futures.immediateFailedFuture(new IOException(e));
    }
  } else {
    return Futures.immediateFailedFuture(t);
  }
}
 
Example #3
Source File: Retrier.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Executes an {@link AsyncCallable}, retrying execution in case of failure with the given
 * backoff.
 */
public <T> ListenableFuture<T> executeAsync(AsyncCallable<T> call, Backoff backoff) {
  try {
    return Futures.catchingAsync(
        call.call(),
        Exception.class,
        t -> onExecuteAsyncFailure(t, call, backoff),
        MoreExecutors.directExecutor());
  } catch (Exception e) {
    return onExecuteAsyncFailure(e, call, backoff);
  }
}
 
Example #4
Source File: Retrier.java    From bazel with Apache License 2.0 5 votes vote down vote up
private <T> ListenableFuture<T> onExecuteAsyncFailure(
    Exception t, AsyncCallable<T> call, Backoff backoff) {
  long waitMillis = backoff.nextDelayMillis();
  if (waitMillis >= 0 && isRetriable(t)) {
    try {
      return Futures.scheduleAsync(
          () -> executeAsync(call, backoff), waitMillis, TimeUnit.MILLISECONDS, retryService);
    } catch (RejectedExecutionException e) {
      // May be thrown by .scheduleAsync(...) if i.e. the executor is shutdown.
      return Futures.immediateFailedFuture(new IOException(e));
    }
  } else {
    return Futures.immediateFailedFuture(t);
  }
}
 
Example #5
Source File: JavaAsync.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Finds factorial of a number using Guava's Futures.submitAsync()
 * @param number
 * @return
 */
@Loggable
public static ListenableFuture<Long> factorialUsingGuavaFutures(int number) {
    ListeningExecutorService service = MoreExecutors.listeningDecorator(threadpool);
    AsyncCallable<Long> asyncCallable = Callables.asAsyncCallable(new Callable<Long>() {
        public Long call() {
            return factorial(number);
        }
    }, service);
    return Futures.submitAsync(asyncCallable, service);
}
 
Example #6
Source File: Retrier.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
/** Executes an {@link AsyncCallable}, retrying execution in case of failure. */
public <T> ListenableFuture<T> executeAsync(AsyncCallable<T> call) {
  return executeAsync(call, newBackoff());
}
 
Example #7
Source File: Retrier.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** Executes an {@link AsyncCallable}, retrying execution in case of failure. */
public <T> ListenableFuture<T> executeAsync(AsyncCallable<T> call) {
  return executeAsync(call, newBackoff());
}