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:com.spotify.apollo.concurrent.Util.java

public static <T> ListenableFuture<T> asFuture(CompletionStage<T> stage) {
    SettableFuture<T> future = SettableFuture.create();

    stage.whenComplete((result, throwable) -> {
        if (throwable != null) {
            future.setException(throwable);
        } else {/*from   ww  w .j a v  a 2  s.  com*/
            future.set(result);
        }
    });

    return future;
}

From source file:com.google.jimfs.JimfsAsynchronousFileChannel.java

/**
 * Immediate future indicating that the channel is closed.
 *//*from  w  w w  .j  a  v  a  2s.co m*/
private static <V> ListenableFuture<V> closedChannelFuture() {
    SettableFuture<V> future = SettableFuture.create();
    future.setException(new ClosedChannelException());
    return future;
}

From source file:com.github.javadev.undescriptive.client.AsyncClient.java

private static <T> ListenableFuture<T> execute(final Class<T> clazz, final BoundRequestBuilder request) {
    final SettableFuture<T> guavaFut = SettableFuture.create();
    try {/*w  ww  .  j av a  2s .  com*/
        request.execute(new GuavaFutureConverter<T>(clazz, guavaFut));
    } catch (final IOException e) {
        guavaFut.setException(e);
    }
    return guavaFut;
}

From source file:me.j360.trace.storage.elasticsearch.ElasticFutures.java

static <T> ListenableFuture<T> toGuava(ListenableActionFuture<T> elasticFuture) {
    final SettableFuture<T> future = SettableFuture.create();
    elasticFuture.addListener(new ActionListener<T>() {
        @Override//from ww  w.jav  a 2  s . c o m
        public void onResponse(T t) {
            future.set(t);
        }

        @Override
        public void onFailure(Throwable e) {
            future.setException(e);
        }
    });
    return future;
}

From source file:c5db.ConcurrencyTestUtil.java

private static ListenableFuture<Boolean> runAndReturnCompletionFuture(ExecutorService executor,
        IndexedExceptionThrowingRunnable runnable, int invocationIndex) {
    final SettableFuture<Boolean> setWhenFinished = SettableFuture.create();

    executor.execute(() -> {/*from ww w.j av a  2s  .  c o  m*/
        try {
            runnable.run(invocationIndex);
            setWhenFinished.set(true);
        } catch (Throwable t) {
            setWhenFinished.setException(t);
        }
    });
    return setWhenFinished;
}

From source file:org.apache.heron.statemgr.FileSystemStateManager.java

protected static <V> void safeSetException(SettableFuture<V> future, Throwable cause) {
    if (!future.setException(cause)) {
        LOG.warning("Unexpected - a local settable future is set twice!");
    }/*from w  w  w.  j a  va2  s . c  o m*/
}

From source file:io.v.v23.syncbase.nosql.NoSql.java

private static ListenableFuture<Boolean> tryBatch(final VContext ctx, final Database db,
        final BatchOptions opts, final BatchOperation op) {
    final SettableFuture<Boolean> ret = SettableFuture.create();
    Futures.addCallback(db.beginBatch(ctx, opts), new FutureCallback<BatchDatabase>() {
        @Override/*ww w  . j a  v a 2 s.c o  m*/
        public void onFailure(Throwable t) {
            ret.setException(t);
        }

        @Override
        public void onSuccess(final BatchDatabase batch) {
            Futures.addCallback(op.run(batch), new FutureCallback<Void>() {
                @Override
                public void onFailure(final Throwable t) {
                    Futures.addCallback(batch.abort(ctx), new FutureCallback<Void>() {
                        @Override
                        public void onSuccess(Void result) {
                            ret.setException(t);
                        }

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

                @Override
                public void onSuccess(Void result) {
                    Futures.addCallback(batch.commit(ctx), new FutureCallback<Void>() {
                        @Override
                        public void onSuccess(Void result) {
                            ret.set(true); // success
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            if (t instanceof ConcurrentBatchException) {
                                // retry
                                ret.set(false);
                            } else {
                                ret.setException(t);
                            }
                        }
                    });
                }
            });
        }
    });
    return ret;
}

From source file:io.v.v23.syncbase.Batch.java

@CheckReturnValue
private static ListenableFuture<Boolean> tryBatch(final VContext ctx, final Database db,
        final BatchOptions opts, final BatchOperation op) {
    final SettableFuture<Boolean> ret = SettableFuture.create();
    Futures.addCallback(db.beginBatch(ctx, opts), new FutureCallback<BatchDatabase>() {
        @Override/*from   w w w  .j av  a 2 s.  com*/
        public void onFailure(Throwable t) {
            ret.setException(t);
        }

        @Override
        public void onSuccess(final BatchDatabase batch) {
            Futures.addCallback(op.run(batch), new FutureCallback<Void>() {
                @Override
                public void onFailure(final Throwable t) {
                    Futures.addCallback(batch.abort(ctx), new FutureCallback<Void>() {
                        @Override
                        public void onSuccess(Void result) {
                            ret.setException(t);
                        }

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

                @Override
                public void onSuccess(Void result) {
                    Futures.addCallback(opts.getReadOnly() ? batch.abort(ctx) : batch.commit(ctx),
                            new FutureCallback<Void>() {
                                @Override
                                public void onSuccess(Void result) {
                                    ret.set(true); // success
                                }

                                @Override
                                public void onFailure(Throwable t) {
                                    if (t instanceof ConcurrentBatchException) {
                                        // retry
                                        ret.set(false);
                                    } else {
                                        ret.setException(t);
                                    }
                                }
                            });
                }
            });
        }
    });
    return ret;
}

From source file:co.cask.cdap.common.async.AsyncFunctions.java

/**
 * Converts a {@link Function} into {@link AsyncFunction} by performing the operation in the given executor.
 *
 * @param function Function to apply/*from   w w  w.j av  a2  s . c  o  m*/
 * @param executor Executor for the function to execute in
 * @param <I> Input type
 * @param <O> Output type
 * @return A {@link AsyncFunction} that will call the function in the given executor.
 */
public static <I, O> AsyncFunction<I, O> asyncWrap(final Function<I, O> function, final Executor executor) {
    return new AsyncFunction<I, O>() {
        @Override
        public ListenableFuture<O> apply(final I input) throws Exception {
            final SettableFuture<O> resultFuture = SettableFuture.create();
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        resultFuture.set(function.apply(input));
                    } catch (Throwable t) {
                        resultFuture.setException(t);
                    }
                }
            });
            return resultFuture;
        }
    };
}

From source file:org.loadui.testfx.utils.FXTestUtils.java

/**
 * Runs the given Callable in the JavaFX thread, waiting for it to complete before returning.
 * Also attempts to wait for any other JavaFX events that may have been queued in the Callable
 * to complete. If any Exception is thrown during execution of the Callable, that exception
 * will be re-thrown from invokeAndWait.
 *
 * @param task//from w ww.  jav a 2  s. co m
 * @param timeoutInSeconds
 * @throws Exception
 */
public static void invokeAndWait(final Callable<?> task, int timeoutInSeconds) throws Exception {
    final SettableFuture<Void> future = SettableFuture.create();

    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            try {
                task.call();
                future.set(null);
            } catch (Throwable e) {
                future.setException(e);
            }
        }
    });

    try {
        future.get(timeoutInSeconds, TimeUnit.SECONDS);
        awaitEvents();
    } catch (ExecutionException exception) {
        if (exception.getCause() instanceof Exception) {
            throw (Exception) exception.getCause();
        } else {
            throw exception;
        }
    }
}