Skip to content

Support CompletableFuture<> as return type of @Async methods [SPR-13128] #17719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Jun 15, 2015 · 9 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jun 15, 2015

Thomas Darimont opened SPR-13128 and commented

Currently @Async annotated methods support to return Future<> result wrappers via AsyncResult<> it would be great if we would support CompletableFuture here as well.

I created a small example that adds the necessary functionality to AsyncExecutionInterceptor.

Usage:

	@Async
	public <T> CompletableFuture<T> asyncOperation(T input) {

		System.out.println("working...");
		LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));
		System.out.println("done.");
		
		return CompletableFuture.completedFuture(input);
	}

Consumer:

service.asyncOperation("test").thenAccept(System.out::println);

Reference URL: https://gist.github.com/thomasdarimont/197e92e537554634adbd

Issue Links:

Referenced from: commits dd4bc63, c41779f, 581ab18

0 votes, 7 watchers

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Good idea, and a nice fit with our existing CompletableFuture work for 4.2! Rolled into 4.2 RC2 now.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Gary Russell commented

Also see spring-projects/spring-integration#1463 for CompletableFuture return types from Spring Integration gateway (proxy) methods.

@spring-projects-issues
Copy link
Collaborator Author

Thomas Darimont commented

Thanks for adding that :)

Would it be possible to get that backported to 4.1.x?
We could add support for that to Spring Data as well but we currently test against 4.1.x.

Cheers,
Thomas

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

I'm afraid not since it's a new feature which benefits from going through a release candidate phase... It also ties nicely into our general CompletableFuture theme in 4.2.

I talked about this with Ollie yesterday, and there seems to be nothing preventing you from shipping CompletableFuture support in Spring Data even against SPR 4.1.7...

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Reopening since this appears to break AnnotationAsyncExecutionAspectTests.qualifiedAsyncMethodsAreRoutedToCorrectExecutor().

See failing Performance builds for details:

java.lang.ClassCastException: java.lang.ClassCastException: java.util.concurrent.FutureTask cannot be cast to java.util.concurrent.CompletableFuture
java.lang.ClassCastException: java.util.concurrent.FutureTask cannot be cast to java.util.concurrent.CompletableFuture
	at org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspectTests$ClassWithQualifiedAsyncMethods.e1OtherWork(AnnotationAsyncExecutionAspectTests.java:287)
	at org.springframework.scheduling.aspectj.AnnotationAsyncExecutionAspectTests.qualifiedAsyncMethodsAreRoutedToCorrectExecutor(AnnotationAsyncExecutionAspectTests.java:147)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
(40 more lines...)

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Oops, I added a test for the AspectJ variant of processing here but apparently forgot to put the actual code for it into the aspect implementation... So effectively, just the interceptor-based variant worked. Fixed now, with the common CompletableFuture processing happening in a shared base class instead of just in AsyncExecutionInterceptor.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Aha! Glad you sorted it out with a late night coding session... just before 4.2 RC2. ;)

@spring-projects-issues
Copy link
Collaborator Author

Didier Loiseau commented

Is the support of CompletableFuture with @Async documented somewhere? I was trying to find documentation and the minimum version that supports it but I only found this ticket and a few blog posts.

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Nov 4, 2016

Oliver Drotbohm commented

Good one. I've filed #19447 to improve the documentation on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants