Interface ThreadContextExecutorService
- All Superinterfaces:
Executor
,ExecutorService
- All Known Implementing Classes:
DefaultThreadContextExecutorService
Allows the execution of
Runnable
and Callable
within a ThreadContext
within a
ExecutorService
.
The ThreadContext
properties will be automatically propagated to the asynchronous task (either
Runnable
or Callable
) if the current Thread
already contains a ThreadContext
.
For example:
final DefaultThreadContextExecutorService executor =
DefaultThreadContextExecutorService.of(Executors.newCachedThreadPool());
final Future<Try<Tenant>> tenant =
executor.submit(
// code that is executed within the ThreadContext created by DefaultThreadContextExecutorService
TenantAccessor::tryGetCurrentTenant);
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoid
execute
(Runnable command, ThreadContext threadContext) Attach aThreadContext
to the given command and execute it.void
execute
(Runnable command, ThreadContextExecutor threadContextExecutor) Attach aThreadContextExecutor
to the given command and execute it.Future<?>
submit
(Runnable task, ThreadContext threadContext) Attach aThreadContext
to the given task and execute it.Future<?>
submit
(Runnable task, ThreadContextExecutor threadContextExecutor) Attach aThreadContextExecutor
to the given task and execute it.<T> Future<T>
submit
(Runnable task, T result, ThreadContext threadContext) Attach aThreadContext
to the given task and execute it.<T> Future<T>
submit
(Runnable task, T result, ThreadContextExecutor threadContextExecutor) Attach aThreadContextExecutor
to the given task and execute it.<T> Future<T>
submit
(Callable<T> task, ThreadContext threadContext) Attach aThreadContext
to the given task and execute it.<T> Future<T>
submit
(Callable<T> task, ThreadContextExecutor threadContextExecutor) Attach aThreadContextExecutor
to the given task and execute it.Methods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit
-
Method Details
-
execute
Attach aThreadContext
to the given command and execute it.- Parameters:
command
- the runnable taskthreadContext
- theThreadContext
to attach to the command- See Also:
-
execute
Attach aThreadContextExecutor
to the given command and execute it.- Parameters:
command
- the runnable taskthreadContextExecutor
- theThreadContextExecutor
to attach to the command- See Also:
-
submit
Attach aThreadContext
to the given task and execute it.- Type Parameters:
T
- the type of the task's result- Parameters:
task
- the task to submitthreadContext
- theThreadContext
to attach to the task- Returns:
- a Future representing pending completion of the task
- See Also:
-
submit
@Nonnull <T> Future<T> submit(@Nonnull Callable<T> task, @Nonnull ThreadContextExecutor threadContextExecutor) Attach aThreadContextExecutor
to the given task and execute it.- Type Parameters:
T
- the type of the task's result- Parameters:
task
- the task to submitthreadContextExecutor
- theThreadContextExecutor
to attach to the task- Returns:
- a Future representing pending completion of the task
- See Also:
-
submit
@Nonnull <T> Future<T> submit(@Nonnull Runnable task, @Nonnull T result, @Nonnull ThreadContext threadContext) Attach aThreadContext
to the given task and execute it.- Type Parameters:
T
- the type of the task's result- Parameters:
task
- the task to submitthreadContext
- theThreadContext
to attach to the taskresult
- the result to return- Returns:
- a Future representing pending completion of the task
- See Also:
-
submit
@Nonnull <T> Future<T> submit(@Nonnull Runnable task, @Nonnull T result, @Nonnull ThreadContextExecutor threadContextExecutor) Attach aThreadContextExecutor
to the given task and execute it.- Type Parameters:
T
- the type of the task's result- Parameters:
task
- the task to submitthreadContextExecutor
- theThreadContextExecutor
to attach to the taskresult
- the result to return- Returns:
- a Future representing pending completion of the task
- See Also:
-
submit
Attach aThreadContext
to the given task and execute it.- Parameters:
task
- the task to submitthreadContext
- theThreadContext
to attach to the task- Returns:
- a Future representing pending completion of the task
- See Also:
-
submit
@Nonnull Future<?> submit(@Nonnull Runnable task, @Nonnull ThreadContextExecutor threadContextExecutor) Attach aThreadContextExecutor
to the given task and execute it.- Parameters:
task
- the task to submitthreadContextExecutor
- theThreadContextExecutor
to attach to the task- Returns:
- a Future representing pending completion of the task
- See Also:
-