Class ContextManagers
- java.lang.Object
-
- nl.talsmasoftware.context.ContextManagers
-
public final class ContextManagers extends Object
Utility class to allow concurrent systems totake snapshots of all contexts
from knownContextManager
implementations.Such a
snapshot
can be passed to a background task to allow the context to bereactivated
in that background thread, until it getsclosed
again (preferably in atry-with-resources
construct).- Author:
- Sjoerd Talsma
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
clearActiveContexts()
Clears all active contexts from the current thread.static ContextSnapshot
createContextSnapshot()
This method is able to create a 'snapshot' from the currentactive context
from all knownContextManager
implementations.static <T> void
onActivate(Class<? extends ContextManager<? super T>> contextManager, T activatedContextValue, T previousContextValue)
Notifies all context observers for the specifiedcontextManager
about the activated context value.static <T> void
onDeactivate(Class<? extends ContextManager<? super T>> contextManager, T deactivatedContextValue, T restoredContextValue)
Notifies all context observers for the specifiedcontextManager
about the deactivated context value.static void
useClassLoader(ClassLoader classLoader)
Override the ClassLoader used to lookup contextmanagers.
-
-
-
Method Detail
-
createContextSnapshot
public static ContextSnapshot createContextSnapshot()
This method is able to create a 'snapshot' from the currentactive context
from all knownContextManager
implementations.This snapshot is returned as a single object that can be temporarily
reactivated
. Don't forget toclose
the reactivated context once you're done, preferably in atry-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 specifiedcontextManager
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 ornull
if no value was activated.previousContextValue
- The previous context value ornull
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 specifiedcontextManager
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 valuerestoredContextValue
- The restored context value ornull
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. Specifynull
to restore the default behaviour.- Since:
- 1.0.5
-
-