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

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

Introduction

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

Prototype

@GwtIncompatible("TODO")
@CheckReturnValue
public static <V> ListenableFuture<V> nonCancellationPropagating(ListenableFuture<V> future) 

Source Link

Document

Creates a new ListenableFuture whose result is set from the supplied future when it completes.

Usage

From source file:com.facebook.buck.util.concurrent.ResourcePool.java

/**
 * @param executorService where to perform the resource processing. Should really be a "real"
 *                        executor (not a directExecutor).
 * @return a {@link ListenableFuture} containing the result of the processing. The future will be
 *         cancelled if the {@link ResourcePool#close()} method is called.
 *//*from w  w w. j av  a2 s.com*/
public synchronized <T> ListenableFuture<T> scheduleOperationWithResource(ThrowingFunction<R, T> withResource,
        final ListeningExecutorService executorService) {
    Preconditions.checkState(!closing.get());

    final ListenableFuture<T> futureWork = Futures.transformAsync(initialSchedule(),
            new AsyncFunction<Void, T>() {
                @Override
                public ListenableFuture<T> apply(Void input) throws Exception {
                    Either<R, ListenableFuture<Void>> resourceRequest = requestResource();
                    if (resourceRequest.isLeft()) {
                        R resource = resourceRequest.getLeft();
                        boolean resourceIsDefunct = false;
                        try {
                            return Futures.immediateFuture(withResource.apply(resource));
                        } catch (Exception e) {
                            resourceIsDefunct = (resourceUsageErrorPolicy == ResourceUsageErrorPolicy.RETIRE);
                            throw e;
                        } finally {
                            returnResource(resource, resourceIsDefunct);
                        }
                    } else {
                        return Futures.transformAsync(resourceRequest.getRight(), this, executorService);
                    }
                }
            }, executorService);

    pendingWork.add(futureWork);
    futureWork.addListener(() -> {
        synchronized (ResourcePool.this) {
            pendingWork.remove(futureWork);
        }
    }, executorService);

    // If someone else calls cancel on `futureWork` it makes it impossible to wait for that future
    // to finish using the resource, which then makes shutdown code exit too early.
    return Futures.nonCancellationPropagating(futureWork);
}

From source file:com.google.gerrit.lucene.AbstractLuceneIndex.java

private ListenableFuture<?> submit(Callable<Long> task) {
    ListenableFuture<Long> future = Futures.nonCancellationPropagating(writerThread.submit(task));
    return Futures.transformAsync(future, gen -> {
        // Tell the reopen thread a future is waiting on this
        // generation so it uses the min stale time when refreshing.
        reopenThread.waitForGeneration(gen, 0);
        return new NrtFuture(gen);
    }, directExecutor());/* w w w . j  a  v a2 s  .  c  om*/
}

From source file:com.google.gerrit.server.index.change.ChangeIndexer.java

private static <T> CheckedFuture<T, IOException> submit(Callable<T> task, ListeningExecutorService executor) {
    return Futures.makeChecked(Futures.nonCancellationPropagating(executor.submit(task)), MAPPER);
}