Package pl.morgwai.base.guice.scopes
Class TrackableContext<ContextT extends TrackableContext<ContextT>>
- java.lang.Object
-
- pl.morgwai.base.guice.scopes.InjectionContext
-
- pl.morgwai.base.guice.scopes.TrackableContext<ContextT>
-
- All Implemented Interfaces:
Serializable
public abstract class TrackableContext<ContextT extends TrackableContext<ContextT>> extends InjectionContext
AnInjectionContext
that canexecute tasks within itself
, so that it can be tracked acrossThreads
using its associatedContextTracker
.Subclasses must use themselves as
ContextT
type argument.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
TrackableContext(ContextTracker<ContextT> tracker)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
executeWithinAll(List<TrackableContext<?>> contexts, Runnable task)
static <T> T
executeWithinAll(List<TrackableContext<?>> contexts, Callable<T> task)
Executestask
synchronously on the currentThread
within allcontexts
.void
executeWithinSelf(Runnable task)
<T> T
executeWithinSelf(Callable<T> task)
Asks the associatedContextTracker
to set thisContext
as the current one for the callingThread
and executestask
synchronously.protected void
setTracker(ContextTracker<ContextT> tracker)
Sets aContextTracker
that will be used for tracking thisContext
.-
Methods inherited from class pl.morgwai.base.guice.scopes.InjectionContext
prepareForSerialization, produceIfAbsent, removeScopedObject, restoreAfterDeserialization
-
-
-
-
Constructor Detail
-
TrackableContext
protected TrackableContext(ContextTracker<ContextT> tracker)
-
-
Method Detail
-
executeWithinSelf
public <T> T executeWithinSelf(Callable<T> task) throws Exception
Asks the associatedContextTracker
to set thisContext
as the current one for the callingThread
and executestask
synchronously. Afterwards clears the current context.- Throws:
Exception
- See Also:
executeWithinAll(List, Callable)
-
executeWithinSelf
public void executeWithinSelf(Runnable task)
-
executeWithinAll
public static <T> T executeWithinAll(List<TrackableContext<?>> contexts, Callable<T> task) throws Exception
Executestask
synchronously on the currentThread
within allcontexts
. Used to transferContexts
saved withContextTracker.getActiveContexts(List)
after dispatching to anotherThread
.- Throws:
Exception
-
executeWithinAll
public static void executeWithinAll(List<TrackableContext<?>> contexts, Runnable task)
-
setTracker
protected void setTracker(ContextTracker<ContextT> tracker)
Sets aContextTracker
that will be used for tracking thisContext
. This method must be called once immediately after each deserialization of thisContext
.- Throws:
IllegalStateException
- if a tracker for thisContext
has already been set.
-
-