Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use Future/Futuretask objects with Spring TaskExecutors?

Is it possible to use Java FutureTask with a Spring TaskExecutor to get a Future object?

I'm looking for a TaskExecutor that implements the Java ExecutorService interface, in particular the submit() method. Looking through the Spring Javadocs doesn't reveal any classes like this. Is there some alternate method to handle futures through Spring TaskExecutors that I am unaware of?

If it is possible, could you also include an example?

like image 389
James McMahon Avatar asked May 19 '09 15:05

James McMahon


People also ask

What's the difference between future and FutureTask in Java?

Future is a base interface and defines the abstraction of an object which promises results to be available in the future while FutureTask is an implementation of the Future interface.

What is the use of TaskExecutor in Spring?

The TaskExecutor was originally created to give other Spring components an abstraction for thread pooling where needed. Components such as the ApplicationEventMulticaster , JMS's AbstractMessageListenerContainer , and Quartz integration all use the TaskExecutor abstraction to pool threads.

How to create Future object in Java?

The easiest way to create an ExecutorService is to use one of the factory methods of the Executors class. After the creation of the asynchronous task, a Java Future object is returned from the executor. If you'd like to read more about The Executor Framework, we've got an in-depth article on that.

What is a future How is it used in ExecutorService?

An example of using Future is working with Thread pools. When one submit a task to ExecutorService which is take a long running time, then it returns a Future object immediately. This Future object can be used for task completion and getting result of computation.


1 Answers

Spring 3 has added submit methods with support for Future objects to AsyncTaskExecutor. Until then if you want access to Future objects I think you will need to get the underlying JDK executor (e.g. using getThreadPoolExecutor) and submit tasks directly on that.

like image 53
Mark Avatar answered Sep 19 '22 11:09

Mark