Class ContextTrackingExecutor

  • All Implemented Interfaces:
    Executor

    public class ContextTrackingExecutor
    extends Object
    implements Executor
    An executor that automatically updates which thread runs within which TrackableContext when executing a task. By default backed by a fixed size ThreadPoolExecutor.

    Instances usually correspond 1-1 with some type of blocking or time consuming operations, such as CPU intensive calculations or blocking network communication with some resource.
    In case of network operations, a given threadPool size should usually correspond to the pool size of the connections to the given resource.
    In case of CPU intensive operations, it should usually correspond to the number of cores available to the process (Runtime.availableProcessors()).

    Instances are usually created at app startup, stored on static vars and/or bound for injection with a specific name:

     bind(ContextTrackingExecutor.class)
         .annotatedWith(Names.named("someOpTypeExecutor"))
         .toInstance(...)

    and then injected using @Named annotation:

     @Named("someOpTypeExecutor")
     ContextTrackingExecutor someOpTypeExecutor

    At app shutdown shutdown() followed by enforceTermination(long, TimeUnit) should be called on every instance.

    If multiple threads run within the same context, then the attributes they access must be thread-safe or properly synchronized.