Package nl.talsmasoftware.context
Interface ContextSnapshot
-
public interface ContextSnapshot
Interface for a 'snapshot' that can capture the (then-) 'active context' from all known registeredContextManager
implementations.
Obtain a new snapshot by callingContextManagers.createContextSnapshot()
which will include a snapshot for all supportedContext
types through their respectivemanagers
.This allows for a generic method to:
- Create a new snapshot of the active context (for all registered ContextManagers).
- Pass the returned
ContextSnapshot
along to a background job. - Allow the background job to (temporary)
reactivate
the snapshot for some required code path. - The reactivation is also a
Context
of its own. Although it does not return any specificvalue
, it must beclosed
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 Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Context<Void>
reactivate()
This method activates all contained values by the snapshot in their respectiveContext
implementations.
-
-
-
Method Detail
-
reactivate
Context<Void> reactivate()
This method activates all contained values by the snapshot in their respectiveContext
implementations.The reactivated context is of type
Void
, because it does not contain any value itself. It closes all contained snapshot values collectively from itsclose()
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).
-
-