List of usage examples for com.google.common.util.concurrent Futures withFallback
@Deprecated @CheckReturnValue public static <V> ListenableFuture<V> withFallback(ListenableFuture<? extends V> input, FutureFallback<? extends V> fallback)
From source file:dagger2.producers.internal.Producers.java
/** * Returns a future of {@link Produced} that represents the completion (either success or failure) * of the given future. If the input future succeeds, then the resulting future also succeeds with * a successful {@code Produced}; if the input future fails, then the resulting future succeeds * with a failing {@code Produced}./* www. ja v a 2 s .c om*/ * * <p>Cancelling the resulting future will propagate the cancellation to the input future; but * cancelling the input future will trigger the resulting future to succeed with a failing * {@code Produced}. */ // TODO(user): Document what happens with an InterruptedException after you figure out how to // trigger one in a test. public static <T> ListenableFuture<Produced<T>> createFutureProduced(ListenableFuture<T> future) { return Futures.withFallback(Futures.transform(future, new Function<T, Produced<T>>() { @Override public Produced<T> apply(final T value) { return new Produced<T>() { @Override public T get() { return value; } }; } }), Producers.<T>futureFallbackForProduced()); }
From source file:io.v.impl.google.lib.discovery.DiscoveryImpl.java
@Override public ListenableFuture<Void> advertise(VContext ctx, Advertisement ad, List<BlessingPattern> visibility) throws VException { ListenableFutureCallback<Void> cb = new ListenableFutureCallback<>(); nativeAdvertise(nativeRef, ctx, ad, visibility, cb); return Futures.withFallback(cb.getFuture(ctx), new FutureFallback<Void>() { public ListenableFuture<Void> create(Throwable t) { if (t instanceof CancellationException) { return Futures.immediateFuture(null); }//from w ww .j a v a 2 s .com return Futures.immediateFailedFuture(t); } }); }
From source file:io.crate.executor.transport.task.AsyncChainedTask.java
protected AsyncChainedTask() { result = SettableFuture.create();//from w ww. jav a2s. com ListenableFuture<TaskResult> resultFallback = Futures.withFallback(result, new FutureFallback<TaskResult>() { @Override public ListenableFuture<TaskResult> create(@Nonnull Throwable t) throws Exception { return Futures.immediateFuture((TaskResult) RowCountResult.error(t)); } }); resultList = new ArrayList<>(); resultList.add(resultFallback); }
From source file:org.robotninjas.concurrent.FluentFutureTask.java
@Override public FluentFuture<V> withFallback(FutureFallback<V> fallback) { return new FluentDecorator<>(Futures.withFallback(this, fallback)); }
From source file:org.onos.yangtools.yang.model.repo.util.AbstractSchemaRepository.java
private static final <T extends SchemaSourceRepresentation> CheckedFuture<T, SchemaSourceException> fetchSource( final SourceIdentifier id, final Iterator<AbstractSchemaSourceRegistration<?>> it) { final AbstractSchemaSourceRegistration<?> reg = it.next(); @SuppressWarnings("unchecked") final CheckedFuture<? extends T, SchemaSourceException> f = ((SchemaSourceProvider<T>) reg.getProvider()) .getSource(id);//from w ww . jav a2s. c om return Futures.makeChecked(Futures.withFallback(f, new FutureFallback<T>() { @Override public ListenableFuture<T> create(final Throwable t) throws SchemaSourceException { LOG.debug("Failed to acquire source from {}", reg, t); if (it.hasNext()) { return fetchSource(id, it); } throw new MissingSchemaSourceException("All available providers exhausted", id, t); } }), FETCH_MAPPER); }
From source file:org.opendaylight.yangtools.yang.model.repo.util.AbstractSchemaRepository.java
private static <T extends SchemaSourceRepresentation> CheckedFuture<T, SchemaSourceException> fetchSource( final SourceIdentifier id, final Iterator<AbstractSchemaSourceRegistration<?>> it) { final AbstractSchemaSourceRegistration<?> reg = it.next(); @SuppressWarnings("unchecked") final CheckedFuture<? extends T, SchemaSourceException> f = ((SchemaSourceProvider<T>) reg.getProvider()) .getSource(id);//from w w w . j a v a 2s . c o m return Futures.makeChecked(Futures.withFallback(f, new FutureFallback<T>() { @Override public ListenableFuture<T> create(@Nonnull final Throwable t) throws SchemaSourceException { LOG.debug("Failed to acquire source from {}", reg, t); if (it.hasNext()) { return fetchSource(id, it); } throw new MissingSchemaSourceException("All available providers exhausted", id, t); } }), FETCH_MAPPER); }
From source file:io.v.rx.syncbase.RxTable.java
private <T> Observable<T> getInitial(final BatchDatabase db, final String tableName, final String key, final TypeToken<T> tt, final T defaultValue) { @SuppressWarnings("unchecked") final ListenableFuture<T> fromGet = (ListenableFuture<T>) db.getTable(tableName).get(mVContext, key, tt == null ? Object.class : tt.getType()); return toObservable(Futures.withFallback(fromGet, t -> t instanceof NoExistException ? Futures.immediateFuture(defaultValue) : Futures.immediateFailedFuture(t))); }
From source file:com.spotify.trickle.PreparedGraph.java
private ListenableFuture<R> future(final TraverseState state) { final ImmutableList.Builder<ListenableFuture<?>> futuresListBuilder = builder(); // get node and value dependencies for (Dep<?> input : graph.getInputs()) { final ListenableFuture<?> inputFuture = input.getFuture(state); futuresListBuilder.add(inputFuture); }//from www . jav a 2 s . c o m final ImmutableList<ListenableFuture<?>> futures = futuresListBuilder.build(); final TraverseState.FutureCallInformation currentCall = state.record(this, futures); // future for signaling propagation - needs to include predecessors, too List<ListenableFuture<?>> mustHappenBefore = Lists.newArrayList(futures); for (Graph<?> predecessor : graph.getPredecessors()) { mustHappenBefore.add(state.futureForGraph(predecessor)); } final ListenableFuture<List<Object>> allFuture = allAsList(mustHappenBefore); checkArgument(graph.getInputs().size() == futures.size(), "sanity check result: insane"); return Futures.withFallback(nodeFuture(futures, allFuture, state.getExecutor()), new NodeExecutionFallback<R>(graph, currentCall, state)); }
From source file:dk.ilios.spanner.internal.ExperimentingSpannerRun.java
/** * Schedule all the trials./*from ww w .j a v a 2s. c om*/ * <p> * <p>This method arranges all the {@link ScheduledTrial trials} to run according to their * scheduling criteria. The executor instance is responsible for enforcing max parallelism. */ private List<ListenableFuture<Trial.Result>> scheduleTrials(List<ScheduledTrial> trials, final ListeningExecutorService executor) { List<ListenableFuture<Trial.Result>> pendingTrials = Lists.newArrayList(); List<ScheduledTrial> serialTrials = Lists.newArrayList(); for (final ScheduledTrial scheduledTrial : trials) { if (scheduledTrial.policy() == TrialSchedulingPolicy.PARALLEL) { pendingTrials.add(executor.submit(scheduledTrial.trialTask())); } else { serialTrials.add(scheduledTrial); } } // A future representing the completion of all prior tasks. Futures.successfulAsList allows us // to ignore failure. ListenableFuture<?> previous = Futures.successfulAsList(pendingTrials); for (final ScheduledTrial scheduledTrial : serialTrials) { // each of these trials can only start after all prior trials have finished, so we use // Futures.transform to force the sequencing. ListenableFuture<Trial.Result> current = Futures.transform(previous, new AsyncFunction<Object, Trial.Result>() { @Override public ListenableFuture<Trial.Result> apply(Object ignored) { return executor.submit(scheduledTrial.trialTask()); } }); pendingTrials.add(current); // ignore failure of the prior task. previous = Futures.withFallback(current, FALLBACK_TO_NULL); } return pendingTrials; }
From source file:com.google.caliper.runner.ExperimentingCaliperRun.java
/** * Schedule all the trials./*from ww w. jav a 2s . c o m*/ * * <p>This method arranges all the {@link ScheduledTrial trials} to run according to their * scheduling criteria. The executor instance is responsible for enforcing max parallelism. */ private List<ListenableFuture<TrialResult>> scheduleTrials(List<ScheduledTrial> trials, final ListeningExecutorService executor) { List<ListenableFuture<TrialResult>> pendingTrials = Lists.newArrayList(); List<ScheduledTrial> serialTrials = Lists.newArrayList(); for (final ScheduledTrial scheduledTrial : trials) { if (scheduledTrial.policy() == TrialSchedulingPolicy.PARALLEL) { pendingTrials.add(executor.submit(scheduledTrial.trialTask())); } else { serialTrials.add(scheduledTrial); } } // A future representing the completion of all prior tasks. Futures.successfulAsList allows us // to ignore failure. ListenableFuture<?> previous = Futures.successfulAsList(pendingTrials); for (final ScheduledTrial scheduledTrial : serialTrials) { // each of these trials can only start after all prior trials have finished, so we use // Futures.transform to force the sequencing. ListenableFuture<TrialResult> current = Futures.transform(previous, new AsyncFunction<Object, TrialResult>() { @Override public ListenableFuture<TrialResult> apply(Object ignored) { return executor.submit(scheduledTrial.trialTask()); } }); pendingTrials.add(current); // ignore failure of the prior task. previous = Futures.withFallback(current, FALLBACK_TO_NULL); } return pendingTrials; }