Example usage for org.springframework.util.concurrent ListenableFuture isCancelled

List of usage examples for org.springframework.util.concurrent ListenableFuture isCancelled

Introduction

In this page you can find the example usage for org.springframework.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:com.opopov.cloud.image.utils.Utils.java

public static <T> CompletableFuture<T> fromListenableFuture(ListenableFuture<T> listenable) {
    CompletableFuture<T> completable = new CompletableFuture<T>() {

        @Override/*from w w  w.j  a  v  a  2 s  .  c  o  m*/
        public boolean isCancelled() {
            return listenable.isCancelled();
        }

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            //delegate cancel to the wrapped listenable
            boolean cancelledStatus = listenable.cancel(mayInterruptIfRunning);
            super.cancel(mayInterruptIfRunning);
            return cancelledStatus;
        }
    };

    //now delegate the callbacks
    ListenableFutureCallback<T> callback = new ListenableFutureCallback<T>() {
        @Override
        public void onFailure(Throwable ex) {
            //delegate exception
            completable.completeExceptionally(ex);
        }

        @Override
        public void onSuccess(T result) {
            //delegate success
            completable.complete(result);
        }
    };

    listenable.addCallback(callback);

    return completable;

}

From source file:org.springframework.cloud.sleuth.instrument.web.client.TraceAsyncRestTemplate.java

@Override
protected <T> ListenableFuture<T> doExecute(URI url, HttpMethod method, AsyncRequestCallback requestCallback,
        ResponseExtractor<T> responseExtractor) throws RestClientException {
    final ListenableFuture<T> future = super.doExecute(url, method, requestCallback, responseExtractor);
    final Span span = this.tracer.getCurrentSpan();
    future.addCallback(new TraceListenableFutureCallback<>(this.tracer, span));
    // potential race can happen here
    if (span != null && span.equals(this.tracer.getCurrentSpan())) {
        this.tracer.detach(span);
    }//  w  ww  . ja  v a 2s .  c om
    return new ListenableFuture<T>() {

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

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

        @Override
        public boolean isDone() {
            return future.isDone();
        }

        @Override
        public T get() throws InterruptedException, ExecutionException {
            return future.get();
        }

        @Override
        public T get(long timeout, TimeUnit unit)
                throws InterruptedException, ExecutionException, TimeoutException {
            return future.get(timeout, unit);
        }

        @Override
        public void addCallback(ListenableFutureCallback<? super T> callback) {
            future.addCallback(new TraceListenableFutureCallbackWrapper<>(TraceAsyncRestTemplate.this.tracer,
                    span, callback));
        }

        @Override
        public void addCallback(SuccessCallback<? super T> successCallback, FailureCallback failureCallback) {
            future.addCallback(
                    new TraceSuccessCallback<>(TraceAsyncRestTemplate.this.tracer, span, successCallback),
                    new TraceFailureCallback(TraceAsyncRestTemplate.this.tracer, span, failureCallback));
        }
    };
}