Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.2 @Async task with return type of Future

Tags:

I am trying to implement a spring @Async task which has a return type of Future, but I can't really figure out how to do it properly.

  1. What will I gain by doing so ? will I now have a controll over my task so I can stop it and run it ?
  2. Is there any reference implementation on how I can do it ? springsource does not provide any.

EDIT

From spring source and spring refrence manual :

Even methods that return a value can be invoked asynchronously. However, such methods are required to have a Future typed return value. This still provides the benefit of asynchronous execution so that the caller can perform other tasks prior to calling get() on that Future.

and the it gives an example like so :

@Async
Future<String> returnSomething(int i) {
// this will be executed asynchronously
}

How to implement this correctly ?

like image 587
engma Avatar asked Jun 22 '13 11:06

engma


People also ask

What will happen if you specify @async over a public method of a Spring bean?

Simply put, annotating a method of a bean with @Async will make it execute in a separate thread. In other words, the caller will not wait for the completion of the called method. One interesting aspect in Spring is that the event support in the framework also has support for async processing if necessary.

What does @async do in Spring?

The @EnableAsync annotation switches on Spring's ability to run @Async methods in a background thread pool. This class also customizes the Executor by defining a new bean. Here, the method is named taskExecutor , since this is the specific method name for which Spring searches.

What is use of @EnableAsync?

EnableAsync is used for configuration and enable Spring's asynchronous method execution capability, it should not be put on your Service or Component class, it should be put on your Configuration class like: @Configuration @EnableAsync public class AppConfig { }

What is asynchronous programming Java?

Asynchronous programming in Java is a technique for parallel programming that lets teams distribute work and build application features separately from the primary application thread. Then, when the team is ready with the feature, the code is synced with the main thread.


2 Answers

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.

like image 188
Petr Avatar answered Nov 03 '22 19:11

Petr


Check out this blog post.

Important configuration is:

  1. @Async on Spring managed bean method.
  2. 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().

like image 45
shane lee Avatar answered Nov 03 '22 20:11

shane lee