List of usage examples for com.google.common.util.concurrent ListenableFuture addListener
void addListener(Runnable listener, Executor executor);
From source file:dk.ilios.spanner.internal.ExperimentingSpannerRun.java
public static <T> ImmutableList<ListenableFuture<T>> inCompletionOrder( Iterable<? extends ListenableFuture<? extends T>> futures) { final ConcurrentLinkedQueue<SettableFuture<T>> delegates = Queues.newConcurrentLinkedQueue(); ImmutableList.Builder<ListenableFuture<T>> listBuilder = ImmutableList.builder(); Executor executor = MoreExecutors.sameThreadExecutor(); for (final ListenableFuture<? extends T> future : futures) { SettableFuture<T> delegate = SettableFuture.create(); // Must make sure to add the delegate to the queue first in case the future is already done delegates.add(delegate);/*from w ww . j a v a 2s . c o m*/ future.addListener(new Runnable() { @Override public void run() { SettableFuture<T> delegate = delegates.remove(); try { delegate.set(Uninterruptibles.getUninterruptibly(future)); } catch (ExecutionException e) { delegate.setException(e.getCause()); } catch (CancellationException e) { delegate.cancel(true); } } }, executor); listBuilder.add(delegate); } return listBuilder.build(); }
From source file:com.spotify.folsom.client.DefaultRawMemcacheClient.java
private static <T> ListenableFuture<T> onExecutor(ListenableFuture<T> future, Executor executor) { if (executor == null) { return future; }/*w w w . j a va 2 s . c o m*/ CallbackSettableFuture<T> newFuture = new CallbackSettableFuture<>(future); future.addListener(newFuture, executor); return newFuture; }
From source file:com.facebook.presto.operator.Driver.java
private static ListenableFuture<?> firstFinishedFuture(List<ListenableFuture<?>> futures) { SettableFuture<?> result = SettableFuture.create(); ExecutorService executor = MoreExecutors.newDirectExecutorService(); for (ListenableFuture<?> future : futures) { future.addListener(() -> result.set(null), executor); }//from ww w .j a va 2s .c o m return result; }
From source file:io.prestosql.operator.Driver.java
private static ListenableFuture<?> firstFinishedFuture(List<ListenableFuture<?>> futures) { if (futures.size() == 1) { return futures.get(0); }/*from w w w .j a v a 2 s. c o m*/ SettableFuture<?> result = SettableFuture.create(); for (ListenableFuture<?> future : futures) { future.addListener(() -> result.set(null), directExecutor()); } return result; }
From source file:me.lucko.luckperms.bukkit.migration.MigrationPowerfulPerms.java
private static void getPlayerPermissions(PermissionManager manager, UUID uuid, Consumer<List<Permission>> callback) { if (legacy) { try {/* www . j a v a 2s. c o m*/ getPlayerPermissionsMethod.invoke(manager, uuid, new LPResultRunnable<List<Permission>>() { @Override public void run() { callback.accept(getResult()); } }); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } else { try { ListenableFuture<List<Permission>> lf = (ListenableFuture<List<Permission>>) getPlayerPermissionsMethod .invoke(manager, uuid); try { if (lf.isDone()) { callback.accept(lf.get()); } else { lf.addListener(() -> { try { callback.accept(lf.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }, Runnable::run); } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } }
From source file:me.lucko.luckperms.commands.migration.subcommands.MigrationPowerfulPerms.java
private static void getPlayerPermissions(PermissionManager manager, UUID uuid, Callback<List<Permission>> callback) { if (legacy) { try {//from w w w . jav a2 s .co m getPlayerPermissionsMethod.invoke(manager, uuid, new LPResultRunnable<List<Permission>>() { @Override public void run() { callback.onComplete(getResult()); } }); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } else { try { ListenableFuture<List<Permission>> lf = (ListenableFuture<List<Permission>>) getPlayerPermissionsMethod .invoke(manager, uuid); try { if (lf.isDone()) { callback.onComplete(lf.get()); } else { lf.addListener(() -> { try { callback.onComplete(lf.get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } }, Runnable::run); } } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } }
From source file:com.facebook.buck.remoteexecution.grpc.GrpcAsyncBlobFetcher.java
private <T> ListenableFuture<T> closeScopeWhenFutureCompletes(Scope scope, ListenableFuture<T> future) { future.addListener(() -> scope.close(), MoreExecutors.directExecutor()); return future; }
From source file:org.jenkinsci.plugins.workflow.support.concurrent.Futures.java
/** * <p>Returns a new {@code ListenableFuture} whose result is asynchronously * derived from the result of the given {@code Future}. More precisely, the * returned {@code Future} takes its result from a {@code Future} produced by * applying the given {@code Function} to the result of the original {@code * Future}. Example:// w ww . j ava2 s. c o m * * <pre> {@code * ListenableFuture<RowKey> rowKeyFuture = indexService.lookUp(query); * Function<RowKey, ListenableFuture<QueryResult>> queryFunction = * new Function<RowKey, ListenableFuture<QueryResult>>() { * public ListenableFuture<QueryResult> apply(RowKey rowKey) { * return dataService.read(rowKey); * } * }; * ListenableFuture<QueryResult> queryFuture = * chain(rowKeyFuture, queryFunction, executor); * }</pre> * * <p>The returned {@code Future} attempts to keep its cancellation state in * sync with that of the input future and that of the future returned by the * chain function. That is, if the returned {@code Future} is cancelled, it * will attempt to cancel the other two, and if either of the other two is * cancelled, the returned {@code Future} will receive a callback in which it * will attempt to cancel itself. * * <p>Note: For cases in which the work of creating the derived future is * fast and lightweight, consider {@linkplain Futures#chain(ListenableFuture, * Function) the other overload} or explicit use of {@code * sameThreadExecutor}. For heavier derivations, this choice carries some * caveats: First, the thread that the derivation runs in depends on whether * the input {@code Future} is done at the time {@code chain} is called. In * particular, if called late, {@code chain} will run the derivation in the * thread that called {@code chain}. Second, derivations may run in an * internal thread of the system responsible for the input {@code Future}, * such as an RPC network thread. Finally, during the execution of a {@code * sameThreadExecutor} {@code chain} function, all other registered but * unexecuted listeners are prevented from running, even if those listeners * are to run in other executors. * * @param input The future to chain * @param function A function to chain the results of the provided future * to the results of the returned future. * @param executor Executor to run the function in. * @return A future that holds result of the chain. * @deprecated Convert your {@code Function} to a {@code AsyncFunction}, and * use {@link #transform(ListenableFuture, AsyncFunction, Executor)}. This * method is scheduled to be removed from Guava in Guava release 12.0. */ @Deprecated /*package*/ static <I, O> ListenableFuture<O> chain(ListenableFuture<I> input, final Function<? super I, ? extends ListenableFuture<? extends O>> function, Executor executor) { checkNotNull(function); ChainingListenableFuture<I, O> chain = new ChainingListenableFuture<I, O>(new AsyncFunction<I, O>() { @Override /* * All methods of ListenableFuture are covariant, and we don't expose * the object anywhere that would allow it to be downcast. */ @SuppressWarnings("unchecked") public ListenableFuture<O> apply(I input) { return (ListenableFuture) function.apply(input); } }, input); input.addListener(chain, executor); return chain; }
From source file:org.dcache.cells.FutureReply.java
public FutureReply(ListenableFuture<? extends T> future) { this.future = future; future.addListener(this, MoreExecutors.directExecutor()); }
From source file:io.airlift.drift.client.stats.JmxMethodInvocationStat.java
@Override public void recordResult(long startTime, ListenableFuture<Object> result) { result.addListener(() -> { time.add(nanosSince(startTime)); try {//from w ww . j av a2 s.c o m result.get(); successes.update(1); } catch (Throwable throwable) { failures.update(1); } }, directExecutor()); }