Example usage for com.google.common.util.concurrent ListenableFuture isCancelled

List of usage examples for com.google.common.util.concurrent ListenableFuture isCancelled

Introduction

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

Prototype

boolean isCancelled();

Source Link

Document

Returns true if this task was cancelled before it completed normally.

Usage

From source file:dagger.functional.producers.cancellation.CancellationModule.java

@Produces
@Named("ep3")/*from  w  w w . ja  va  2 s. com*/
static ListenableFuture<String> produceEntryPoint3(Producer<String> dependencyProducer) {
    ListenableFuture<String> dependencyFuture = dependencyProducer.get();
    assertThat(dependencyFuture.isDone()).isFalse();
    assertThat(dependencyFuture.cancel(true)).isTrue();
    assertThat(dependencyFuture.isCancelled()).isTrue();
    return dependencyFuture;
}

From source file:org.robotninjas.barge.rpc.netty.RpcChannelFactory.java

@Override
public void destroyObject(Object key, ListenableFuture<NettyRpcChannel> obj) throws Exception {
    if (obj.isDone() && !obj.isCancelled()) {
        obj.get().close();// w w  w  .  j  a  v  a2s  .  c o m
    } else {
        obj.cancel(false);
    }
}

From source file:zipkin2.storage.cassandra.internal.call.ResultSetFutureCall.java

@Override
protected final boolean doIsCanceled() {
    ListenableFuture<ResultSet> maybeFuture = future;
    return maybeFuture != null && maybeFuture.isCancelled();
}

From source file:com.android.tools.idea.run.DeviceFutures.java

/** @return the target devices, if all are now ready. Otherwise, null. */
@Nullable//from   w w  w  . j a v  a  2 s. c o m
public List<IDevice> getIfReady() {
    List<ListenableFuture<IDevice>> devices = get();

    for (ListenableFuture<IDevice> deviceFuture : devices) {
        if (!deviceFuture.isDone() || deviceFuture.isCancelled()) {
            return null;
        }
    }

    try {
        return Futures.getChecked(Futures.allAsList(devices), ExecutionException.class);
    } catch (Exception e) {
        // This can happen if the process behind the future threw an exception.
        return null;
    }
}

From source file:dagger.functional.producers.cancellation.CancellationSubcomponentModule.java

@Produces
@Named("subTask2")
ListenableFuture<String> produceSubTask2(@Named("foo") String foo, Producer<String> dependency) {
    ListenableFuture<String> dependencyFuture = dependency.get();
    assertThat(dependencyFuture.cancel(true)).isTrue();
    assertThat(dependencyFuture.isCancelled()).isTrue();
    return tester.start("subTask2");
}

From source file:dagger.functional.producers.cancellation.CancellationModule.java

@Produces
@Named("baz")// w  w w . j a v  a  2s  . c  o  m
ListenableFuture<String> produceBaz(@Named("foo") Producer<String> foo, @Named("bar") String bar) {
    ListenableFuture<String> fooFuture = foo.get();
    if (!fooFuture.isDone()) {
        assertThat(fooFuture.cancel(true)).isTrue();
        assertThat(fooFuture.isCancelled()).isTrue();
    }
    return tester.start("baz");
}

From source file:flipkart.lego.engine.Lego.java

private void cancelFutures(Collection<ListenableFuture> futures) {
    for (ListenableFuture future : futures) {
        try {//from  ww  w  . j  a v  a  2 s.c om
            if (!future.isCancelled() && !future.isDone()) {
                future.cancel(true);
            }
        } catch (Exception ignored) {
        }
    }
}

From source file:dagger.producers.internal.DependencyMethodProducer.java

@Override
public final Producer<T> newEntryPointView(final CancellationListener cancellationListener) {
    return new Producer<T>() {
        private final Set<ListenableFuture<T>> entryPointFutures = Collections
                .newSetFromMap(new MapMaker().weakKeys().<ListenableFuture<T>, Boolean>makeMap());

        @Override//from  w  w  w.java 2 s  .  co  m
        public ListenableFuture<T> get() {
            final ListenableFuture<T> future = DependencyMethodProducer.this.get();
            if (!future.isDone() && entryPointFutures.add(future)) {
                future.addListener(new Runnable() {
                    @Override
                    public void run() {
                        entryPointFutures.remove(future);
                        if (future.isCancelled()) {
                            // TODO(cgdecker): Make this also propagate the actual value that was passed for
                            // mayInterruptIfRunning
                            cancellationListener.onProducerFutureCancelled(true);
                        }
                    }
                }, directExecutor());
            }
            return future;
        }
    };
}

From source file:bio.gcat.batch.Batch.java

public ListenableFuture<Result> execute(Collection<Tuple> tuples) {
    final Result result = new Result(tuples);
    Queue<Action> queue = new LinkedList<>(actions);
    if (queue.isEmpty())
        return new DefiniteListenableFuture<>(result);

    Action action;/*from ww w .j  a va  2 s .  c o m*/
    Future<Collection<Tuple>> future = new DefiniteFuture<>(tuples);
    ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
    while ((action = queue.poll()) != null)
        future = service.submit(InjectionLogger.injectLogger(result, action.new Task(future)));

    final ListenableFuture<Collection<Tuple>> lastFuture = (ListenableFuture<Collection<Tuple>>) future;
    return new ListenableFuture<Result>() {
        @Override
        public boolean isDone() {
            return lastFuture.isDone();
        }

        @Override
        public boolean isCancelled() {
            return lastFuture.isCancelled();
        }

        @Override
        public Result get(long timeout, TimeUnit unit)
                throws InterruptedException, ExecutionException, TimeoutException {
            result.setTuples(lastFuture.get(timeout, unit));
            return result;
        }

        @Override
        public Result get() throws InterruptedException, ExecutionException {
            result.setTuples(lastFuture.get());
            return result;
        }

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            return lastFuture.cancel(mayInterruptIfRunning);
        }

        @Override
        public void addListener(Runnable listener, Executor executor) {
            lastFuture.addListener(listener, executor);
        }
    };
}

From source file:co.cask.cdap.app.runtime.spark.SparkRuntimeService.java

@Override
protected void run() throws Exception {
    ListenableFuture<RunId> jobCompletion = completion.getAndSet(submitSpark.call());
    // If the jobCompletion is not null, meaning the stop() was called before the atomic reference "completion" has been
    // updated. This mean the job is cancelled. We also need to cancel the future returned by submitSpark.call().
    if (jobCompletion != null) {
        completion.get().cancel(true);/*from  www  .  ja v a  2 s .  c o  m*/
    } else {
        // It's possible that the completion reference is changed by the triggerShutdown call between the getAndSet()
        // and the get() call here. But it's ok since the triggeredShutdown will always put a cancelled future in the
        // atomic reference and cancel the actual one.
        jobCompletion = completion.get();
    }

    try {
        // Block for job completion
        jobCompletion.get();
    } catch (Exception e) {
        // See if it is due to job cancelation. If it is, then it's not an error.
        if (jobCompletion.isCancelled()) {
            LOG.info("Spark program execution cancelled: {}", runtimeContext);
        } else {
            throw e;
        }
    }
}