Example usage for com.google.common.util.concurrent FutureFallback FutureFallback

List of usage examples for com.google.common.util.concurrent FutureFallback FutureFallback

Introduction

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

Prototype

FutureFallback

Source Link

Usage

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 w  w  .  ja  v a 2s  . c  o m
            return Futures.immediateFailedFuture(t);
        }
    });
}

From source file:io.crate.executor.transport.task.AsyncChainedTask.java

protected AsyncChainedTask() {
    result = SettableFuture.create();/*from   ww  w.  j a  v a 2 s.  co  m*/
    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.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);/*  w  w w.java2  s  .c  o  m*/

    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  2 s  .c  om*/

    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:com.spotify.helios.client.HeliosClient.java

public ListenableFuture<VersionResponse> version() {
    // Create a fallback in case we fail to connect to the master. Return null if this happens.
    // The transform below will handle this and return an appropriate error message to the caller.
    final ListenableFuture<Response> futureWithFallback = withFallback(request(uri("/version/"), "GET"),
            new FutureFallback<Response>() {
                @Override/*from  ww w.j a  v a  2 s  .  c  o  m*/
                public ListenableFuture<Response> create(@NotNull Throwable t) throws Exception {
                    return immediateFuture(null);
                }
            });

    return transform(futureWithFallback, new AsyncFunction<Response, VersionResponse>() {
        @Override
        public ListenableFuture<VersionResponse> apply(@NotNull Response reply) throws Exception {
            final String masterVersion = reply == null ? "Unable to connect to master"
                    : reply.status() == HTTP_OK ? Json.read(reply.payload(), String.class)
                            : "Master replied with error code " + reply.status();

            return immediateFuture(new VersionResponse(Version.POM_VERSION, masterVersion));
        }
    });
}

From source file:com.spotify.helios.testing.HeliosSoloDeployment.java

private <T> T getOrNull(final ListenableFuture<T> future) throws ExecutionException, InterruptedException {
    return Futures.withFallback(future, new FutureFallback<T>() {
        @Override/*from  www .java 2  s.  com*/
        public ListenableFuture<T> create(@NotNull final Throwable t) throws Exception {
            return Futures.immediateFuture(null);
        }
    }).get();
}

From source file:org.rhq.enterprise.server.cloud.StorageNodeManagerBean.java

private ListenableFuture<StorageNodeLoadComposite> wrapFuture(ListenableFuture<StorageNodeLoadComposite> future,
        final StorageNodeLoadComposite value, final String msg) {
    return Futures.withFallback(future, new FutureFallback<StorageNodeLoadComposite>() {
        @Override/* w w  w  .j a  v a2  s. c om*/
        public ListenableFuture<StorageNodeLoadComposite> create(Throwable t) throws Exception {
            if (log.isDebugEnabled()) {
                log.debug(msg, t);
            } else {
                log.info(msg + ": " + t.getMessage());
            }
            return Futures.immediateFuture(value);
        }
    });
}

From source file:com.spotify.helios.system.SystemTestBase.java

protected <T> T getOrNull(final ListenableFuture<T> future) throws ExecutionException, InterruptedException {
    return Futures.withFallback(future, new FutureFallback<T>() {
        @Override/*from  ww w  .  j  a  v a2 s.  co m*/
        public ListenableFuture<T> create(@NotNull final Throwable t) throws Exception {
            return Futures.immediateFuture(null);
        }
    }).get();
}