Class TaskExecutorAdapter
java.lang.Object
org.springframework.core.task.support.TaskExecutorAdapter
- All Implemented Interfaces:
Executor,AsyncListenableTaskExecutor,AsyncTaskExecutor,TaskExecutor
Adapter that takes a JDK
java.util.concurrent.Executor and
exposes a Spring TaskExecutor for it.
Also detects an extended java.util.concurrent.ExecutorService, adapting
the AsyncTaskExecutor interface accordingly.- Since:
- 3.0
- Author:
- Juergen Hoeller
- See Also:
-
Field Summary
Fields inherited from interface org.springframework.core.task.AsyncTaskExecutor
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE -
Constructor Summary
ConstructorsConstructorDescriptionTaskExecutorAdapter(Executor concurrentExecutor) Create a new TaskExecutorAdapter, using the given JDK concurrent executor. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoExecute(Executor concurrentExecutor, TaskDecorator taskDecorator, Runnable runnable) Actually execute the givenRunnable(which may be a user-supplied task or a wrapper around a user-supplied task) with the given executor.voidDelegates to the specified JDK concurrent executor.voidDeprecated.final voidsetTaskDecorator(TaskDecorator taskDecorator) Specify a customTaskDecoratorto be applied to anyRunnableabout to be executed.Future<?>Submit a Runnable task for execution, receiving a Future representing that task.<T> Future<T>Submit a Callable task for execution, receiving a Future representing that task.submitListenable(Runnable task) Submit aRunnabletask for execution, receiving aListenableFuturerepresenting that task.<T> ListenableFuture<T>submitListenable(Callable<T> task) Submit aCallabletask for execution, receiving aListenableFuturerepresenting that task.
-
Constructor Details
-
TaskExecutorAdapter
Create a new TaskExecutorAdapter, using the given JDK concurrent executor.- Parameters:
concurrentExecutor- the JDK concurrent executor to delegate to
-
-
Method Details
-
setTaskDecorator
Specify a customTaskDecoratorto be applied to anyRunnableabout to be executed.Note that such a decorator is not necessarily being applied to the user-supplied
Runnable/Callablebut rather to the actual execution callback (which may be a wrapper around the user-supplied task).The primary use case is to set some execution context around the task's invocation, or to provide some monitoring/statistics for task execution.
NOTE: Exception handling in
TaskDecoratorimplementations is limited to plainRunnableexecution viaexecutecalls. In case of#submitcalls, the exposedRunnablewill be aFutureTaskwhich does not propagate any exceptions; you might have to cast it and callFuture#getto evaluate exceptions.- Since:
- 4.3
-
execute
Delegates to the specified JDK concurrent executor.- Specified by:
executein interfaceExecutor- Specified by:
executein interfaceTaskExecutor- Parameters:
task- theRunnableto execute (nevernull)- See Also:
-
execute
Deprecated.Description copied from interface:AsyncTaskExecutorExecute the giventask.- Specified by:
executein interfaceAsyncTaskExecutor- Parameters:
task- theRunnableto execute (nevernull)startTimeout- the time duration (milliseconds) within which the task is supposed to start. This is intended as a hint to the executor, allowing for preferred handling of immediate tasks. Typical values areAsyncTaskExecutor.TIMEOUT_IMMEDIATEorAsyncTaskExecutor.TIMEOUT_INDEFINITE(the default as used byTaskExecutor.execute(Runnable)).- See Also:
-
submit
Description copied from interface:AsyncTaskExecutorSubmit a Runnable task for execution, receiving a Future representing that task. The Future will return anullresult upon completion.- Specified by:
submitin interfaceAsyncTaskExecutor- Parameters:
task- theRunnableto execute (nevernull)- Returns:
- a Future representing pending completion of the task
-
submit
Description copied from interface:AsyncTaskExecutorSubmit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.- Specified by:
submitin interfaceAsyncTaskExecutor- Parameters:
task- theCallableto execute (nevernull)- Returns:
- a Future representing pending completion of the task
-
submitListenable
Description copied from interface:AsyncListenableTaskExecutorSubmit aRunnabletask for execution, receiving aListenableFuturerepresenting that task. The Future will return anullresult upon completion.- Specified by:
submitListenablein interfaceAsyncListenableTaskExecutor- Parameters:
task- theRunnableto execute (nevernull)- Returns:
- a
ListenableFuturerepresenting pending completion of the task
-
submitListenable
Description copied from interface:AsyncListenableTaskExecutorSubmit aCallabletask for execution, receiving aListenableFuturerepresenting that task. The Future will return the Callable's result upon completion.- Specified by:
submitListenablein interfaceAsyncListenableTaskExecutor- Parameters:
task- theCallableto execute (nevernull)- Returns:
- a
ListenableFuturerepresenting pending completion of the task
-
doExecute
protected void doExecute(Executor concurrentExecutor, @Nullable TaskDecorator taskDecorator, Runnable runnable) throws RejectedExecutionException Actually execute the givenRunnable(which may be a user-supplied task or a wrapper around a user-supplied task) with the given executor.- Parameters:
concurrentExecutor- the underlying JDK concurrent executor to delegate totaskDecorator- the specified decorator to be applied, if anyrunnable- the runnable to execute- Throws:
RejectedExecutionException- if the given runnable cannot be accepted- Since:
- 4.3
-