Package io.micrometer.context
Class ContextExecutorService<EXECUTOR extends ExecutorService>
java.lang.Object
io.micrometer.context.ContextExecutorService<EXECUTOR>
- All Implemented Interfaces:
AutoCloseable
,Executor
,ExecutorService
- Direct Known Subclasses:
ContextScheduledExecutorService
public class ContextExecutorService<EXECUTOR extends ExecutorService>
extends Object
implements ExecutorService
Wraps an
ExecutorService
in order to capture context via
ContextSnapshot
when a task is submitted, and propagate context to the task
when it is executed.- Since:
- 1.0.0
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
ContextExecutorService
(EXECUTOR executorService, Supplier<ContextSnapshot> contextSnapshot) Create an instance ofContextScheduledExecutorService
. -
Method Summary
Modifier and TypeMethodDescriptionboolean
awaitTermination
(long timeout, TimeUnit unit) protected ContextSnapshot
capture()
void
protected EXECUTOR
invokeAll
(Collection<? extends Callable<T>> tasks) invokeAll
(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) <T> T
invokeAny
(Collection<? extends Callable<T>> tasks) <T> T
invokeAny
(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) boolean
boolean
void
shutdown()
Future<?>
<T> Future<T>
<T> Future<T>
static ExecutorService
wrap
(ExecutorService service) Deprecated.static ExecutorService
wrap
(ExecutorService service, ContextSnapshotFactory contextSnapshotFactory) Wrap the givenExecutorService
in order to propagate context to any executed task through the givenContextSnapshotFactory
.static ExecutorService
wrap
(ExecutorService service, Supplier<ContextSnapshot> snapshotSupplier) Wrap the givenExecutorService
in order to propagate context to any executed task through the givenContextSnapshot
supplier.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.concurrent.ExecutorService
close
-
Constructor Details
-
ContextExecutorService
protected ContextExecutorService(EXECUTOR executorService, Supplier<ContextSnapshot> contextSnapshot) Create an instance ofContextScheduledExecutorService
.- Parameters:
executorService
- theExecutorService
to delegate tocontextSnapshot
- supplier of theContextSnapshot
- instruction on who to retrieveContextSnapshot
when tasks are scheduled
-
-
Method Details
-
getExecutorService
-
submit
- Specified by:
submit
in interfaceExecutorService
-
submit
- Specified by:
submit
in interfaceExecutorService
-
submit
- Specified by:
submit
in interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException - Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException - Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException - Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
execute
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdown
in interfaceExecutorService
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminated
in interfaceExecutorService
-
shutdown
public void shutdown()- Specified by:
shutdown
in interfaceExecutorService
-
shutdownNow
- Specified by:
shutdownNow
in interfaceExecutorService
-
awaitTermination
- Specified by:
awaitTermination
in interfaceExecutorService
- Throws:
InterruptedException
-
capture
-
wrap
public static ExecutorService wrap(ExecutorService service, ContextSnapshotFactory contextSnapshotFactory) Wrap the givenExecutorService
in order to propagate context to any executed task through the givenContextSnapshotFactory
.This method only captures ThreadLocal value. To work with other types of contexts, use
wrap(ExecutorService, Supplier)
.- Parameters:
service
- the executorService to wrapcontextSnapshotFactory
-ContextSnapshotFactory
for capturing aContextSnapshot
at the point when tasks are scheduled- Returns:
ExecutorService
wrapper- Since:
- 1.1.2
-
wrap
public static ExecutorService wrap(ExecutorService service, Supplier<ContextSnapshot> snapshotSupplier) Wrap the givenExecutorService
in order to propagate context to any executed task through the givenContextSnapshot
supplier.Typically, a
ContextSnapshotFactory
can be used to supply the snapshot. In the case that only ThreadLocal values are to be captured, thewrap(ExecutorService, ContextSnapshotFactory)
variant can be used.- Parameters:
service
- the executorService to wrapsnapshotSupplier
- supplier for capturing aContextSnapshot
at the point when tasks are scheduled- Returns:
ExecutorService
wrapper
-
wrap
Deprecated.Variant ofwrap(ExecutorService, Supplier)
that usesContextSnapshot.captureAll(Object...)
to create the context snapshot.- Parameters:
service
- the executorService to wrap- Returns:
ExecutorService
wrapper
-
wrap(ExecutorService, Supplier)