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
ConstructorsModifierConstructorDescriptionprotectedContextExecutorService(EXECUTOR executorService, Supplier<ContextSnapshot> contextSnapshot) Create an instance ofContextScheduledExecutorService. -
Method Summary
Modifier and TypeMethodDescriptionbooleanawaitTermination(long timeout, TimeUnit unit) protected ContextSnapshotcapture()voidprotected EXECUTORinvokeAll(Collection<? extends Callable<T>> tasks) invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) <T> TinvokeAny(Collection<? extends Callable<T>> tasks) <T> TinvokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) booleanbooleanvoidshutdown()Future<?> <T> Future<T> <T> Future<T> static ExecutorServicewrap(ExecutorService service) Deprecated.static ExecutorServicewrap(ExecutorService service, ContextSnapshotFactory contextSnapshotFactory) Wrap the givenExecutorServicein order to propagate context to any executed task through the givenContextSnapshotFactory.static ExecutorServicewrap(ExecutorService service, Supplier<ContextSnapshot> snapshotSupplier) Wrap the givenExecutorServicein order to propagate context to any executed task through the givenContextSnapshotsupplier.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.concurrent.ExecutorService
close
-
Constructor Details
-
ContextExecutorService
protected ContextExecutorService(EXECUTOR executorService, Supplier<ContextSnapshot> contextSnapshot) Create an instance ofContextScheduledExecutorService.- Parameters:
executorService- theExecutorServiceto delegate tocontextSnapshot- supplier of theContextSnapshot- instruction on who to retrieveContextSnapshotwhen tasks are scheduled
-
-
Method Details
-
getExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
submit
- Specified by:
submitin interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException - Specified by:
invokeAllin interfaceExecutorService- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException - Specified by:
invokeAnyin interfaceExecutorService- Throws:
InterruptedExceptionExecutionExceptionTimeoutException
-
execute
-
isShutdown
public boolean isShutdown()- Specified by:
isShutdownin interfaceExecutorService
-
isTerminated
public boolean isTerminated()- Specified by:
isTerminatedin interfaceExecutorService
-
shutdown
public void shutdown()- Specified by:
shutdownin interfaceExecutorService
-
shutdownNow
- Specified by:
shutdownNowin interfaceExecutorService
-
awaitTermination
- Specified by:
awaitTerminationin interfaceExecutorService- Throws:
InterruptedException
-
capture
-
wrap
public static ExecutorService wrap(ExecutorService service, ContextSnapshotFactory contextSnapshotFactory) Wrap the givenExecutorServicein 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-ContextSnapshotFactoryfor capturing aContextSnapshotat the point when tasks are scheduled- Returns:
ExecutorServicewrapper- Since:
- 1.1.2
-
wrap
public static ExecutorService wrap(ExecutorService service, Supplier<ContextSnapshot> snapshotSupplier) Wrap the givenExecutorServicein order to propagate context to any executed task through the givenContextSnapshotsupplier.Typically, a
ContextSnapshotFactorycan 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 aContextSnapshotat the point when tasks are scheduled- Returns:
ExecutorServicewrapper
-
wrap
Deprecated.Variant ofwrap(ExecutorService, Supplier)that usesContextSnapshot.captureAll(Object...)to create the context snapshot.- Parameters:
service- the executorService to wrap- Returns:
ExecutorServicewrapper
-
wrap(ExecutorService, Supplier)