Class ContextManagers


  • public final class ContextManagers
    extends Object
    Utility class to allow concurrent systems to take snapshots of all contexts from known ContextManager implementations.

    Such a snapshot can be passed to a background task to allow the context to be reactivated in that background thread, until it gets closed again (preferably in a try-with-resources construct).

    Author:
    Sjoerd Talsma
    • Method Detail

      • createContextSnapshot

        public static ContextSnapshot createContextSnapshot()
        This method is able to create a 'snapshot' from the current active context from all known ContextManager implementations.

        This snapshot is returned as a single object that can be temporarily reactivated. Don't forget to close the reactivated context once you're done, preferably in a try-with-resources construct.

        Returns:
        A new snapshot that can be reactivated elsewhere (e.g. a background thread or even another node) within a try-with-resources construct.
      • clearActiveContexts

        public static void clearActiveContexts()
        Clears all active contexts from the current thread.

        Contexts that are 'stacked' (i.e. restore the previous state upon close) should be closed in a way that includes all 'parent' contexts as well.

        This operation is not intended to be used by general application code as it likely breaks any 'stacked' active context that surrounding code may depend upon. Appropriate use includes thread management, where threads are reused by some pooling mechanism. For example, it is considered safe to clear the context when obtaining a 'fresh' thread from a thread pool (as no context expectations should exist at that point). An even better strategy would be to clear the context right before returning a used thread to the pool as this will allow any unclosed contexts to be garbage collected. Besides preventing contextual issues, this reduces the risk of memory leaks by unbalanced context calls.

        For context managers that are not Clearable and contain an active Context that is not Clearable either, this active context will be closed normally.

      • onActivate

        public static <T> void onActivate​(Class<? extends ContextManager<? super T>> contextManager,
                                          T activatedContextValue,
                                          T previousContextValue)
        Notifies all context observers for the specified contextManager about the activated context value.
        Type Parameters:
        T - The type managed by the context manager.
        Parameters:
        contextManager - The context manager type that activated the context (required to observe).
        activatedContextValue - The activated context value or null if no value was activated.
        previousContextValue - The previous context value or null if unknown or unsupported.
        Since:
        1.0.6
      • onDeactivate

        public static <T> void onDeactivate​(Class<? extends ContextManager<? super T>> contextManager,
                                            T deactivatedContextValue,
                                            T restoredContextValue)
        Notifies all context observers for the specified contextManager about the deactivated context value.
        Type Parameters:
        T - The type managed by the context manager.
        Parameters:
        contextManager - The context manager type that deactivated the context (required to observe).
        deactivatedContextValue - The deactivated context value
        restoredContextValue - The restored context value or null if unknown or unsupported.
        Since:
        1.0.6
      • useClassLoader

        public static void useClassLoader​(ClassLoader classLoader)
        Override the ClassLoader used to lookup contextmanagers.

        Normally, taking a snapshot uses the Context ClassLoader from the current thread to look up all context managers. It is possible to configure a fixed, single classloader in your application for looking up the context managers.

        Using this method to specify a fixed classloader will only impact new context snapshots. Existing snapshots will not be impacted.

        Notes:

        • Please be aware that this configuration is global!
        • This will also affect the lookup of context observers
        Parameters:
        classLoader - The single, fixed ClassLoader to use for finding context managers. Specify null to restore the default behaviour.
        Since:
        1.0.5