AsyncExecutionInterceptor supports Java 8's CompletableFuture as a return type
Issue: SPR-13128
This commit is contained in:
@@ -18,9 +18,12 @@ package org.springframework.aop.interceptor;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.aopalliance.intercept.MethodInterceptor;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
@@ -30,6 +33,7 @@ import org.springframework.core.BridgeMethodResolver;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.task.AsyncListenableTaskExecutor;
|
||||
import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.lang.UsesJava8;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
|
||||
@@ -68,6 +72,11 @@ import org.springframework.util.concurrent.ListenableFuture;
|
||||
public class AsyncExecutionInterceptor extends AsyncExecutionAspectSupport
|
||||
implements MethodInterceptor, Ordered {
|
||||
|
||||
// Java 8's CompletableFuture type present?
|
||||
private static final boolean completableFuturePresent = ClassUtils.isPresent(
|
||||
"java.util.concurrent.CompletableFuture", AsyncExecutionInterceptor.class.getClassLoader());
|
||||
|
||||
|
||||
/**
|
||||
* Create a new {@code AsyncExecutionInterceptor}.
|
||||
* @param defaultExecutor the {@link Executor} (typically a Spring {@link AsyncTaskExecutor}
|
||||
@@ -124,6 +133,12 @@ public class AsyncExecutionInterceptor extends AsyncExecutionAspectSupport
|
||||
};
|
||||
|
||||
Class<?> returnType = invocation.getMethod().getReturnType();
|
||||
if (completableFuturePresent) {
|
||||
Future<Object> result = CompletableFutureDelegate.processCompletableFuture(returnType, task, executor);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
if (ListenableFuture.class.isAssignableFrom(returnType)) {
|
||||
return ((AsyncListenableTaskExecutor) executor).submitListenable(task);
|
||||
}
|
||||
@@ -154,4 +169,29 @@ public class AsyncExecutionInterceptor extends AsyncExecutionAspectSupport
|
||||
return Ordered.HIGHEST_PRECEDENCE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Inner class to avoid a hard dependency on Java 8.
|
||||
*/
|
||||
@UsesJava8
|
||||
private static class CompletableFutureDelegate {
|
||||
|
||||
public static <T> Future<T> processCompletableFuture(Class<?> returnType, final Callable<T> task, Executor executor) {
|
||||
if (!CompletableFuture.class.isAssignableFrom(returnType)) {
|
||||
return null;
|
||||
}
|
||||
return CompletableFuture.supplyAsync(new Supplier<T>() {
|
||||
@Override
|
||||
public T get() {
|
||||
try {
|
||||
return task.call();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new CompletionException(ex);
|
||||
}
|
||||
}
|
||||
}, executor);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user