Example usage for com.google.common.util.concurrent Futures catchingAsync

List of usage examples for com.google.common.util.concurrent Futures catchingAsync

Introduction

In this page you can find the example usage for com.google.common.util.concurrent Futures catchingAsync.

Prototype

@GwtIncompatible("AVAILABLE but requires exceptionType to be Throwable.class")

public static <V, X extends Throwable> ListenableFuture<V> catchingAsync(ListenableFuture<? extends V> input,
        Class<X> exceptionType, AsyncFunction<? super X, ? extends V> fallback, Executor executor) 

Source Link

Document

Returns a Future whose result is taken from the given primary input or, if the primary input fails with the given exceptionType , from the result provided by the fallback .

Usage

From source file:com.facebook.buck.util.network.LocalProcessScribeLogger.java

@Override
public ListenableFuture<Void> log(final String category, final Iterable<String> lines) {
    ListenableFuture<Void> localCatSubmitResult = executorService.submit(new Callable<Void>() {
        @Override/* www .jav a  2s. c  o m*/
        public Void call() throws Exception {
            sendToProcess(category, lines);
            return null;
        }
    });
    return Futures.catchingAsync(localCatSubmitResult, Exception.class, new AsyncFunction<Throwable, Void>() {
        @Override
        public ListenableFuture<Void> apply(Throwable input) throws Exception {
            LOG.info(input, "Submitting to local process failed, falling back to http.");
            return fallback.log(category, lines);
        }
    }, executorService);
}

From source file:com.salesforce.grpc.contrib.FutureChain.java

/**
 * @see Futures#catchingAsync(ListenableFuture, Class, AsyncFunction, Executor)
 *///  ww  w.j a va  2  s  .c o  m
public <E extends Throwable> FutureChain<T> catchingAsync(Class<E> exceptionType,
        AsyncFunction<? super E, ? extends T> fallback) {
    return new FutureChain<>(Futures.catchingAsync(future, exceptionType, fallback, executor), executor);
}

From source file:com.google.cloud.bigtable.grpc.async.RetryingRpcFunction.java

public ListenableFuture<ResponseT> addRetry(final ListenableFuture<ResponseT> future) {
    return Futures.catchingAsync(future, StatusRuntimeException.class, this, retryExecutorService);
}

From source file:com.google.devtools.build.lib.query2.ActionGraphQueryEnvironment.java

@Override
public QueryTaskFuture<Void> getTargetsMatchingPattern(QueryExpression owner, String pattern,
        Callback<ConfiguredTargetValue> callback) {
    TargetPattern patternToEval;//from  w  ww. ja  v  a 2s  .co m
    try {
        patternToEval = getPattern(pattern);
    } catch (TargetParsingException tpe) {
        try {
            reportBuildFileError(owner, tpe.getMessage());
        } catch (QueryException qe) {
            return immediateFailedFuture(qe);
        }
        return immediateSuccessfulFuture(null);
    }
    AsyncFunction<TargetParsingException, Void> reportBuildFileErrorAsyncFunction = exn -> {
        reportBuildFileError(owner, exn.getMessage());
        return Futures.immediateFuture(null);
    };
    return QueryTaskFutureImpl.ofDelegate(Futures.catchingAsync(patternToEval.evalAdaptedForAsync(resolver,
            ImmutableSet.of(), ImmutableSet.of(), (Callback<Target>) partialResult -> {
                List<ConfiguredTargetValue> transformedResult = new ArrayList<>();
                for (Target target : partialResult) {
                    ConfiguredTargetValue configuredTargetValue = getConfiguredTargetValue(target.getLabel());
                    if (configuredTargetValue != null) {
                        transformedResult.add(configuredTargetValue);
                    }
                }
                callback.process(transformedResult);
            }, QueryException.class), TargetParsingException.class, reportBuildFileErrorAsyncFunction,
            MoreExecutors.directExecutor()));
}

From source file:com.google.devtools.build.lib.query2.ConfiguredTargetQueryEnvironment.java

@Override
public QueryTaskFuture<Void> getTargetsMatchingPattern(QueryExpression owner, String pattern,
        Callback<ConfiguredTarget> callback) {
    TargetPattern patternToEval;/*from  w ww  . ja va2s.  c  om*/
    try {
        patternToEval = getPattern(pattern);
    } catch (TargetParsingException tpe) {
        try {
            reportBuildFileError(owner, tpe.getMessage());
        } catch (QueryException qe) {
            return immediateFailedFuture(qe);
        }
        return immediateSuccessfulFuture(null);
    } catch (InterruptedException ie) {
        return immediateCancelledFuture();
    }
    AsyncFunction<TargetParsingException, Void> reportBuildFileErrorAsyncFunction = exn -> {
        reportBuildFileError(owner, exn.getMessage());
        return Futures.immediateFuture(null);
    };
    return QueryTaskFutureImpl.ofDelegate(Futures.catchingAsync(patternToEval.evalAdaptedForAsync(resolver,
            ImmutableSet.of(), ImmutableSet.of(), (Callback<Target>) partialResult -> {
                List<ConfiguredTarget> transformedResult = new ArrayList<>();
                for (Target target : partialResult) {
                    ConfiguredTarget configuredTarget = getConfiguredTarget(target.getLabel());
                    if (configuredTarget != null) {
                        transformedResult.add(configuredTarget);
                    }
                }
                callback.process(transformedResult);
            }, QueryException.class), TargetParsingException.class, reportBuildFileErrorAsyncFunction,
            MoreExecutors.directExecutor()));
}