This is very possible in Spring using @Async task @Async .
First, create a service that will perform the task asynchronously. Here's the @Async annotation, the @Async method that will be scanned and tagged by Spring for asynchronous execution.
import java.util.concurrent.Future; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; @Service public class AsyncTask { @Async public Future<Result> performTask(String someArgument) {
Next, create a component (optional - maybe from any other existing service) that will call the aforementioned service.
import java.util.concurrent.Future; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class AsyncClass { @Autowired private AsyncTask asyncTask; public void doAsyncOperation() throws Exception { List<Future<Result>> futures = new ArrayList<Future<Result>>(); for (int i = 1; i < 10; i++) {
The xml sample configuration needed to enable async is shown below (for annotation-based use @EnableAsync)
<task:annotation-driven executor="myExecutor" /> <task:executor id="myExecutor" pool-size="30" rejection-policy="CALLER_RUNS"/>
For detailed documentation see here
Bond - Java Bond Sep 09 '14 at 10:26 2014-09-09 10:26
source share