Class ConcurrentTaskExecutor
- java.lang.Object
- org.springframework.scheduling.concurrent.ConcurrentTaskExecutor
- All Implemented Interfaces:
Executor
,AsyncListenableTaskExecutor
,AsyncTaskExecutor
,TaskExecutor
,SchedulingTaskExecutor
- Direct Known Subclasses:
ConcurrentTaskScheduler
,DefaultManagedTaskExecutor
public class ConcurrentTaskExecutor extends Object implements AsyncListenableTaskExecutor, SchedulingTaskExecutor
Adapter that takes ajava.util.concurrent.Executor
and exposes a SpringTaskExecutor
for it. Also detects an extendedjava.util.concurrent.ExecutorService
, adapting theAsyncTaskExecutor
interface accordingly.Autodetects a JSR-236
ManagedExecutorService
in order to exposeManagedTask
adapters for it, exposing a long-running hint based onSchedulingAwareRunnable
and an identity name based on the given Runnable/Callable'stoString()
. For JSR-236 style lookup in a Java EE 7 environment, consider usingDefaultManagedTaskExecutor
.Note that there is a pre-built
ThreadPoolTaskExecutor
that allows for defining aThreadPoolExecutor
in bean style, exposing it as a SpringTaskExecutor
directly. This is a convenient alternative to a raw ThreadPoolExecutor definition with a separate definition of the present adapter class.- Since:
- 2.0
- Author:
- Juergen Hoeller
- See Also:
Executor
,ExecutorService
,ThreadPoolExecutor
,Executors
,DefaultManagedTaskExecutor
,ThreadPoolTaskExecutor
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
ConcurrentTaskExecutor.ManagedTaskBuilder
Delegate that wraps a given Runnable/Callable with a JSR-236 ManagedTask, exposing a long-running hint based onSchedulingAwareRunnable
and a given identity name.
Field Summary
Fields inherited from interface org.springframework.core.task.AsyncTaskExecutor
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
Constructor Summary
Constructors Constructor Description ConcurrentTaskExecutor()
Create a new ConcurrentTaskExecutor, using a single thread executor as default.ConcurrentTaskExecutor(Executor concurrentExecutor)
Create a new ConcurrentTaskExecutor, using the givenExecutor
.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
execute(Runnable task)
Execute the giventask
.void
execute(Runnable task, long startTimeout)
Execute the giventask
.Executor
getConcurrentExecutor()
Return theExecutor
that this adapter delegates to.boolean
prefersShortLivedTasks()
This task executor prefers short-lived work units.void
setConcurrentExecutor(Executor concurrentExecutor)
Specify theExecutor
to delegate to.void
setTaskDecorator(TaskDecorator taskDecorator)
Specify a customTaskDecorator
to be applied to anyRunnable
about to be executed.Future<?>
submit(Runnable task)
Submit a Runnable task for execution, receiving a Future representing that task.<T> Future<T>
submit(Callable<T> task)
Submit a Callable task for execution, receiving a Future representing that task.ListenableFuture<?>
submitListenable(Runnable task)
Submit aRunnable
task for execution, receiving aListenableFuture
representing that task.<T> ListenableFuture<T>
submitListenable(Callable<T> task)
Submit aCallable
task for execution, receiving aListenableFuture
representing that task.
Constructor Detail
ConcurrentTaskExecutor
public ConcurrentTaskExecutor()
Create a new ConcurrentTaskExecutor, using a single thread executor as default.- See Also:
Executors.newSingleThreadExecutor()
ConcurrentTaskExecutor
public ConcurrentTaskExecutor(Executor concurrentExecutor)
Create a new ConcurrentTaskExecutor, using the givenExecutor
.Autodetects a JSR-236
ManagedExecutorService
in order to exposeManagedTask
adapters for it.- Parameters:
concurrentExecutor
- theExecutor
to delegate to
Method Detail
setConcurrentExecutor
public final void setConcurrentExecutor(Executor concurrentExecutor)
Specify theExecutor
to delegate to.Autodetects a JSR-236
ManagedExecutorService
in order to exposeManagedTask
adapters for it.
getConcurrentExecutor
public final Executor getConcurrentExecutor()
Return theExecutor
that this adapter delegates to.
setTaskDecorator
public final void setTaskDecorator(TaskDecorator taskDecorator)
Specify a customTaskDecorator
to be applied to anyRunnable
about to be executed.Note that such a decorator is not necessarily being applied to the user-supplied
Runnable
/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
TaskDecorator
implementations is limited to plainRunnable
execution viaexecute
calls. In case of#submit
calls, the exposedRunnable
will be aFutureTask
which does not propagate any exceptions; you might have to cast it and callFuture#get
to evaluate exceptions.- Since:
- 4.3
execute
public void execute(Runnable task)
Description copied from interface:TaskExecutor
Execute the giventask
.The call might return immediately if the implementation uses an asynchronous execution strategy, or might block in the case of synchronous execution.
- Specified by:
execute
in interfaceExecutor
- Specified by:
execute
in interfaceTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)
execute
public void execute(Runnable task, long startTimeout)
Description copied from interface:AsyncTaskExecutor
Execute the giventask
.- Specified by:
execute
in interfaceAsyncTaskExecutor
- Parameters:
task
- theRunnable
to 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_IMMEDIATE
orAsyncTaskExecutor.TIMEOUT_INDEFINITE
(the default as used byTaskExecutor.execute(Runnable)
).
submit
public Future<?> submit(Runnable task)
Description copied from interface:AsyncTaskExecutor
Submit a Runnable task for execution, receiving a Future representing that task. The Future will return anull
result upon completion.- Specified by:
submit
in interfaceAsyncTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)- Returns:
- a Future representing pending completion of the task
submit
public <T> Future<T> submit(Callable<T> task)
Description copied from interface:AsyncTaskExecutor
Submit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.- Specified by:
submit
in interfaceAsyncTaskExecutor
- Parameters:
task
- theCallable
to execute (nevernull
)- Returns:
- a Future representing pending completion of the task
submitListenable
public ListenableFuture<?> submitListenable(Runnable task)
Description copied from interface:AsyncListenableTaskExecutor
Submit aRunnable
task for execution, receiving aListenableFuture
representing that task. The Future will return anull
result upon completion.- Specified by:
submitListenable
in interfaceAsyncListenableTaskExecutor
- Parameters:
task
- theRunnable
to execute (nevernull
)- Returns:
- a
ListenableFuture
representing pending completion of the task
submitListenable
public <T> ListenableFuture<T> submitListenable(Callable<T> task)
Description copied from interface:AsyncListenableTaskExecutor
Submit aCallable
task for execution, receiving aListenableFuture
representing that task. The Future will return the Callable's result upon completion.- Specified by:
submitListenable
in interfaceAsyncListenableTaskExecutor
- Parameters:
task
- theCallable
to execute (nevernull
)- Returns:
- a
ListenableFuture
representing pending completion of the task
prefersShortLivedTasks
public boolean prefersShortLivedTasks()
This task executor prefers short-lived work units.- Specified by:
prefersShortLivedTasks
in interfaceSchedulingTaskExecutor
- Returns:
true
if thisTaskExecutor
prefers short-lived tasks