Class ContextAwareExecutorService

  • All Implemented Interfaces:
    Executor, ExecutorService

    public class ContextAwareExecutorService
    extends CallMappingExecutorService
    Executor service that wraps another executor service, making sure background tasks operates 'within' a context snapshot taken from the submitting thread.

    The executor service will make sure to close the reactivated snapshot again after the code in the task is finished, even if it throws an exception.

    Both Callable and Runnable tasks are mapped.

    Author:
    Sjoerd Talsma
    • Constructor Detail

      • ContextAwareExecutorService

        public ContextAwareExecutorService​(ExecutorService delegate)
    • Method Detail

      • map

        protected <V> Callable<V> map​(Callable<V> callable)
        This method maps any callable (before scheduling it) by taking a snapshot of the context in the scheduling thread and propagating this context into the executed callable by snapshot reactivation.
        Specified by:
        map in class CallMappingExecutorService
        Type Parameters:
        V - the actual return type of the callable object being scheduled.
        Parameters:
        callable - The callable to be mapped.
        Returns:
        A callable that will reactivate the scheduling thread context snapshot before executing.