Interface ContextObserver<T>


  • public interface ContextObserver<T>
    Observe context updates for a particular class of ContextManager.

    Create your context observer by implementing this interface and registering your class to the ServiceLoader SPI by adding the fully qualified class name to the resource /META-INF/services/nl.talsmasoftware.context.observer.ContextObserver.

    It is the responsibility of the context implementor to update observers of context updates. SPI lookup of appropriate observers is facilitated by the ContextManagers.onActivate(Class, Object, Object) and ContextManagers.onDeactivate(Class, Object, Object) utility methods.
    All subclasses of AbstractThreadLocalContext are already observable.

    Author:
    Sjoerd Talsma
    • Method Detail

      • getObservedContextManager

        Class<? extends ContextManager<T>> getObservedContextManager()
        The observed context manager(s).

        Context observers can indicate which type of context manager must be observed using this method. For instance, returning LocaleContextManager.class here, updates for the LocaleContext will be offered to this observer.

        To observe all context updates, return the ContextManager interface class itself, since all context managers must implement it.

        Return null to disable the observer.

        Returns:
        The observed context manager class or null to disable this observer.
      • onActivate

        void onActivate​(T activatedContextValue,
                        T previousContextValue)
        Indicates that a context was just activated.
        Parameters:
        activatedContextValue - The now active context value.
        previousContextValue - The previous context value or null if unknown or unsupported.
      • onDeactivate

        void onDeactivate​(T deactivatedContextValue,
                          T restoredContextValue)
        Indicates that a context was just deactivated.
        Parameters:
        deactivatedContextValue - The deactivated context value.
        restoredContextValue - The now active restored context value or null if unknown or unsupported.