Example usage for com.google.common.util.concurrent SettableFuture setException

List of usage examples for com.google.common.util.concurrent SettableFuture setException

Introduction

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

Prototype

@Override
    public boolean setException(Throwable throwable) 

Source Link

Usage

From source file:io.datakernel.service.ServiceAdapters.java

private static CompletionCallback toCompletionCallback(final SettableFuture<?> future) {
    return new CompletionCallback() {
        @Override/*from   w  w  w  .  j a va 2s . c  om*/
        public void onComplete() {
            future.set(null);
        }

        @Override
        public void onException(Exception exception) {
            future.setException(exception);
        }
    };
}

From source file:com.microsoft.alm.plugin.authentication.AuthHelper.java

/**
 * This method wraps the normal Async call to authenticate and waits on the result.
 *///from  w  ww .  ja v a 2  s .co  m
public static AuthenticationInfo getAuthenticationInfoSynchronously(final AuthenticationProvider provider,
        final String gitRemoteUrl) {
    final SettableFuture<AuthenticationInfo> future = SettableFuture.create();

    provider.authenticateAsync(gitRemoteUrl, new AuthenticationListener() {
        @Override
        public void authenticating() {
            // do nothing
        }

        @Override
        public void authenticated(final AuthenticationInfo authenticationInfo, final Throwable throwable) {
            if (throwable != null) {
                future.setException(throwable);
            } else {
                future.set(authenticationInfo);
            }
        }
    });

    // Wait for the authentication info object to be ready
    // Don't wait any longer than 15 minutes for the user to authenticate
    Throwable t = null;
    try {
        return future.get(15, TimeUnit.MINUTES);
    } catch (InterruptedException ie) {
        t = ie;
    } catch (ExecutionException ee) {
        t = ee;
    } catch (TimeoutException te) {
        t = te;
    } finally {
        if (t != null) {
            logger.error("getAuthenticationInfoSynchronously: failed to get authentication info from user");
            logger.warn("getAuthenticationInfoSynchronously", t);
        }
    }
    return null;
}

From source file:com.continuuity.loom.common.zookeeper.ZKClientExt.java

private static <T> ListenableFuture<T> ignoreError(final ListenableFuture<T> future,
        final Class<? extends KeeperException> ex) {

    final SettableFuture<T> futureWithIgnoredError = SettableFuture.create();
    Futures.addCallback(future, new FutureCallback<T>() {
        @Override//from  www  .j av a  2  s  .  co m
        public void onSuccess(T result) {
            futureWithIgnoredError.set(result);
        }

        @Override
        public void onFailure(Throwable t) {
            if (causedBy(t, ex)) {
                futureWithIgnoredError.set(null);
            } else {
                futureWithIgnoredError.setException(t);
            }
        }
    });

    return futureWithIgnoredError;
}

From source file:io.druid.java.util.common.concurrent.ListenableFutures.java

/**
 * Guava 19 changes the Futures.transform signature so that the async form is different. This is here as a
 * compatability layer until such a time as druid only supports Guava 19 or later, in which case
 * Futures.transformAsync should be used
 *
 * This is NOT copied from guava.//w  w w.ja  va2 s.  c o m
 */
public static <I, O> ListenableFuture<O> transformAsync(final ListenableFuture<I> inFuture,
        final Function<I, ListenableFuture<O>> transform) {
    final SettableFuture<O> finalFuture = SettableFuture.create();
    Futures.addCallback(inFuture, new FutureCallback<I>() {
        @Override
        public void onSuccess(@Nullable I result) {
            final ListenableFuture<O> transformFuture = transform.apply(result);
            Futures.addCallback(transformFuture, new FutureCallback<O>() {
                @Override
                public void onSuccess(@Nullable O result) {
                    finalFuture.set(result);
                }

                @Override
                public void onFailure(Throwable t) {
                    finalFuture.setException(t);
                }
            });
        }

        @Override
        public void onFailure(Throwable t) {
            finalFuture.setException(t);
        }
    });
    return finalFuture;
}

From source file:io.airlift.discovery.client.CachingServiceSelector.java

private static <V> ListenableFuture<V> chainedCallback(ListenableFuture<V> future,
        final FutureCallback<? super V> callback, Executor executor) {
    final SettableFuture<V> done = SettableFuture.create();
    Futures.addCallback(future, new FutureCallback<V>() {
        @Override/*from ww w  .j  a  va2 s  .c om*/
        public void onSuccess(V result) {
            try {
                callback.onSuccess(result);
            } finally {
                done.set(result);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            try {
                callback.onFailure(t);
            } finally {
                done.setException(t);
            }
        }
    }, executor);
    return done;
}

From source file:io.v.v23.InputChannels.java

private static <T> FutureCallback<T> newCallbackForDone(final InputChannel<T> channel,
        final SettableFuture<Void> future, final Executor executor) {
    return new FutureCallback<T>() {
        @Override/*from w  ww .j av a 2s  .com*/
        public void onSuccess(T result) {
            Futures.addCallback(channel.recv(), newCallbackForDone(channel, future, executor), executor);
        }

        @Override
        public void onFailure(Throwable t) {
            if (t instanceof EndOfFileException) {
                future.set(null);
            } else {
                future.setException(t);
            }
        }
    };
}

From source file:com.google.cloud.testing.BaseEmulatorHelper.java

private static int waitForProcess(final Process process, Duration timeout)
        throws InterruptedException, TimeoutException {
    if (process == null) {
        return 0;
    }//from ww w .  j a v a  2s .c  o  m

    final SettableFuture<Integer> exitValue = SettableFuture.create();

    Thread waiter = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                exitValue.set(process.waitFor());
            } catch (InterruptedException e) {
                exitValue.setException(e);
            }
        }
    });
    waiter.start();

    try {
        return exitValue.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        if (e.getCause() instanceof InterruptedException) {
            throw (InterruptedException) e.getCause();
        }
        throw new UncheckedExecutionException(e);
    } finally {
        waiter.interrupt();
    }
}

From source file:com.continuuity.weave.zookeeper.ZKOperations.java

/**
 * Watch for the given path until it exists.
 * @param zkClient The {@link ZKClient} to use.
 * @param path A ZooKeeper path to watch for existent.
 *///from   ww  w  .  jav a  2 s .  c o  m
private static void watchExists(final ZKClient zkClient, final String path,
        final SettableFuture<String> completion) {
    Futures.addCallback(zkClient.exists(path, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (!completion.isDone()) {
                watchExists(zkClient, path, completion);
            }
        }
    }), new FutureCallback<Stat>() {
        @Override
        public void onSuccess(Stat result) {
            if (result != null) {
                completion.set(path);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            completion.setException(t);
        }
    });
}

From source file:com.facebook.buck.remoteexecution.grpc.GrpcRemoteExecutionClients.java

/** Reads a ByteStream onto the arg consumer. */
public static ListenableFuture<Void> readByteStream(String instanceName, Protocol.Digest digest,
        ByteStreamStub byteStreamStub, ThrowingConsumer<ByteString, IOException> dataConsumer) {
    String name = getReadResourceName(instanceName, digest);
    SettableFuture<Void> future = SettableFuture.create();
    byteStreamStub.read(ReadRequest.newBuilder().setResourceName(name).setReadLimit(0).setReadOffset(0).build(),
            new StreamObserver<ReadResponse>() {
                @Override//from   w w w  . j a  va2s.  c o m
                public void onNext(ReadResponse value) {
                    try {
                        dataConsumer.accept(value.getData());
                    } catch (IOException e) {
                        onError(e);
                    }
                }

                @Override
                public void onError(Throwable t) {
                    future.setException(t);
                }

                @Override
                public void onCompleted() {
                    future.set(null);
                }
            });
    return future;
}

From source file:io.v.v23.InputChannels.java

private static <T> FutureCallback<T> newCallbackForList(final InputChannel<T> channel, final List<T> list,
        final SettableFuture<List<T>> future, final Executor executor) {
    return new FutureCallback<T>() {
        @Override/*from   w ww.  j a va2s  . c o  m*/
        public void onSuccess(T result) {
            list.add(result);
            Futures.addCallback(channel.recv(), newCallbackForList(channel, list, future, executor), executor);
        }

        @Override
        public void onFailure(Throwable t) {
            if (t instanceof EndOfFileException) {
                future.set(list);
            } else {
                future.setException(t);
            }
        }
    };
}