Interface ContextSnapshot


  • public interface ContextSnapshot
    Interface for a 'snapshot' that can capture the (then-) 'active context' from all known registered ContextManager implementations.
    Obtain a new snapshot by calling ContextManagers.createContextSnapshot() which will include a snapshot for all supported Context types through their respective managers.

    This allows for a generic method to:

    1. Create a new snapshot of the active context (for all registered ContextManagers).
    2. Pass the returned ContextSnapshot along to a background job.
    3. Allow the background job to (temporary) reactivate the snapshot for some required code path.
    4. The reactivation is also a Context of its own. Although it does not return any specific value, it must be closed when the work requiring the context snapshot is done. This prevents context values leaking in case the used threads are returned to some pool.
    Author:
    Sjoerd Talsma
    • Method Detail

      • reactivate

        Context<Void> reactivate()
        This method activates all contained values by the snapshot in their respective Context implementations.

        The reactivated context is of type Void, because it does not contain any value itself. It closes all contained snapshot values collectively from its close() method.

        Note: The reactivated context must be closed from the same thread that reactivated it. It is the responsibility of the caller to make sure this happens (preferably in a try-with-resources block)
        Using the ContextAwareExecutorService is a safe way to propagate context snapshots without having to worry about closing them.

        Returns:
        A new reactivation context with the snapshot values that will be valid until closed (or new values are registered).