public class TaskExecutorAdapter extends Object implements AsyncListenableTaskExecutor
java.util.concurrent.Executorand exposes a Spring
TaskExecutorfor it. Also detects an extended
java.util.concurrent.ExecutorService, adapting the
|Constructor and Description|
Create a new TaskExecutorAdapter, using the given JDK concurrent executor.
|Modifier and Type||Method and Description|
Actually execute the given
Delegates to the specified JDK concurrent executor.
Execute the given
Submit a Callable task for execution, receiving a Future representing that task.
Submit a Runnable task for execution, receiving a Future representing that task.
public TaskExecutorAdapter(Executor concurrentExecutor)
concurrentExecutor- the JDK concurrent executor to delegate to
public final void setTaskDecorator(TaskDecorator taskDecorator)
TaskDecoratorto be applied to any
Runnableabout to be executed.
Note that such a decorator is not necessarily being applied to the
Callable but 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
is limited to plain
Runnable execution via
In case of
#submit calls, the exposed
Runnable will be a
FutureTask which does not propagate any exceptions; you might
have to cast it and call
Future#get to evaluate exceptions.
public void execute(Runnable task)
public void execute(Runnable task, long startTimeout)
Runnableto execute (never
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 are
AsyncTaskExecutor.TIMEOUT_INDEFINITE(the default as used by
nullresult upon completion.
public ListenableFuture<?> submitListenable(Runnable task)
Runnabletask for execution, receiving a
ListenableFuturerepresenting that task. The Future will return a
nullresult upon completion.
public <T> ListenableFuture<T> submitListenable(Callable<T> task)
Callabletask for execution, receiving a
ListenableFuturerepresenting that task. The Future will return the Callable's result upon completion.
protected void doExecute(Executor concurrentExecutor, @Nullable TaskDecorator taskDecorator, Runnable runnable) throws RejectedExecutionException
Runnable(which may be a user-supplied task or a wrapper around a user-supplied task) with the given executor.
concurrentExecutor- the underlying JDK concurrent executor to delegate to
taskDecorator- the specified decorator to be applied, if any
runnable- the runnable to execute
RejectedExecutionException- if the given runnable cannot be accepted