Spring 3.2 @Async task with return type of Future
Check out this blog post.
Important configuration is:
@Async
on Spring managed bean method.- Enable async in Spring config XML by defining:
<!--
Enables the detection of @Async and @Scheduled annotations
on any Spring-managed object.
-->
<task:annotation-driven />
SimpleAsyncTaskExecutor will be used by default.
Wrap the response in a Future<>
object.
Example
@Async
public Future<PublishAndReturnDocumentResult> generateDocument(FooBarBean bean) {
// do some logic
return new AsyncResult<PublishAndReturnDocumentResult>(result);
}
You can then check if the result is done using result.isDone()
or wait to get the response result.get()
.
Check out this blog post.
Using @Async
allows you to run a computation in a method asynchronously. This means that if it's called (on a Spring managed bean), the control is immediately returned to the caller and the code in the method is run in another thread. The caller receives a Future
object that is bound to the running computation and can use it to check if the computation is running and/or wait for the result.
Creating such a method is simple. Annotate it with @Async
and wrap the result in AsyncResult
, as shown in the blog post.