Example usage for java.util.concurrent CompletableFuture supplyAsync

List of usage examples for java.util.concurrent CompletableFuture supplyAsync

Introduction

In this page you can find the example usage for java.util.concurrent CompletableFuture supplyAsync.

Prototype

public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor) 

Source Link

Document

Returns a new CompletableFuture that is asynchronously completed by a task running in the given executor with the value obtained by calling the given Supplier.

Usage

From source file:org.springframework.aop.interceptor.AsyncExecutionAspectSupport.java

/**
 * Delegate for actually executing the given task with the chosen executor.
 * @param task the task to execute//from   w  ww . j  av a2s  .c om
 * @param executor the chosen executor
 * @param returnType the declared return type (potentially a {@link Future} variant)
 * @return the execution result (potentially a corresponding {@link Future} handle)
 */
@Nullable
protected Object doSubmit(Callable<Object> task, AsyncTaskExecutor executor, Class<?> returnType) {
    if (CompletableFuture.class.isAssignableFrom(returnType)) {
        return CompletableFuture.supplyAsync(() -> {
            try {
                return task.call();
            } catch (Throwable ex) {
                throw new CompletionException(ex);
            }
        }, executor);
    } else if (ListenableFuture.class.isAssignableFrom(returnType)) {
        return ((AsyncListenableTaskExecutor) executor).submitListenable(task);
    } else if (Future.class.isAssignableFrom(returnType)) {
        return executor.submit(task);
    } else {
        executor.submit(task);
        return null;
    }
}

From source file:org.springframework.cloud.sleuth.instrument.async.issues.issue410.Issue410Tests.java

public Span completableFutures() throws ExecutionException, InterruptedException {
    log.info("This task is running with completable future");
    CompletableFuture<Span> span1 = CompletableFuture.supplyAsync(() -> {
        AsyncTask.log.info("First completable future");
        return AsyncTask.this.tracer.getCurrentSpan();
    }, AsyncTask.this.executor);
    CompletableFuture<Span> span2 = CompletableFuture.supplyAsync(() -> {
        AsyncTask.log.info("Second completable future");
        return AsyncTask.this.tracer.getCurrentSpan();
    }, AsyncTask.this.executor);
    CompletableFuture<Span> response = CompletableFuture.allOf(span1, span2).thenApply(ignoredVoid -> {
        AsyncTask.log.info("Third completable future");
        Span joinedSpan1 = span1.join();
        Span joinedSpan2 = span2.join();
        then(joinedSpan2).isNotNull();/*from w  w  w. ja v  a2 s . c  om*/
        then(joinedSpan1).hasTraceIdEqualTo(joinedSpan2.getTraceId());
        AsyncTask.log.info("TraceIds are correct");
        return joinedSpan2;
    });
    this.span.set(response.get());
    return this.span.get();
}

From source file:org.springframework.cloud.sleuth.instrument.async.issues.issue410.Issue410Tests.java

public Span taskScheduler() throws ExecutionException, InterruptedException {
    log.info("This task is running with completable future");
    CompletableFuture<Span> span1 = CompletableFuture.supplyAsync(() -> {
        AsyncTask.log.info("First completable future");
        return AsyncTask.this.tracer.getCurrentSpan();
    }, new LazyTraceExecutor(AsyncTask.this.beanFactory, AsyncTask.this.taskScheduler));
    CompletableFuture<Span> span2 = CompletableFuture.supplyAsync(() -> {
        AsyncTask.log.info("Second completable future");
        return AsyncTask.this.tracer.getCurrentSpan();
    }, new LazyTraceExecutor(AsyncTask.this.beanFactory, AsyncTask.this.taskScheduler));
    CompletableFuture<Span> response = CompletableFuture.allOf(span1, span2).thenApply(ignoredVoid -> {
        AsyncTask.log.info("Third completable future");
        Span joinedSpan1 = span1.join();
        Span joinedSpan2 = span2.join();
        then(joinedSpan2).isNotNull();// w  ww.  j av  a  2s  .c o  m
        then(joinedSpan1).hasTraceIdEqualTo(joinedSpan2.getTraceId());
        AsyncTask.log.info("TraceIds are correct");
        return joinedSpan2;
    });
    this.span.set(response.get());
    return this.span.get();
}