Package pl.morgwai.base.guice.scopes
Class ContextScope<CtxT extends TrackableContext<CtxT>>
- java.lang.Object
-
- pl.morgwai.base.guice.scopes.ContextScope<CtxT>
-
- All Implemented Interfaces:
Scope
- Direct Known Subclasses:
InducedContextScope
public class ContextScope<CtxT extends TrackableContext<CtxT>> extends Object implements Scope
Scopes objects to a context obtained from the associatedContextTracker
.
-
-
Constructor Summary
Constructors Constructor Description ContextScope(String name, ContextTracker<CtxT> tracker)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ServerSideContext
getContext()
String
getName()
<T> Provider<T>
scope(Key<T> key, Provider<T> unscoped)
String
toString()
-
-
-
Constructor Detail
-
ContextScope
public ContextScope(String name, ContextTracker<CtxT> tracker)
-
-
Method Detail
-
getName
public String getName()
-
scope
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped)
- Specified by:
scope
in interfaceScope
- Throws:
RuntimeException
- if there's no context for the current thread. This most commonly happens when providing a callback to some async method without transferring the context. Use static helper methodsContextTrackingExecutor.getActiveContexts(List)
andContextTrackingExecutor.executeWithinAll(java.util.List, Runnable)
to fix it:class MyClass { @Inject ContextTracker<ContextT1> tracker1; @Inject ContextTracker<ContextT2> tracker2; void myMethod(Object param) { // myMethod code var activeCtxList = ContextTrackingExecutor.getActiveContexts(tracker1, tracker2); someAsyncMethod(param, (callbackParam) -> ContextTrackingExecutor.executeWithinAll(activeCtxList, () -> { // callback code } )); } }
-
getContext
protected ServerSideContext getContext()
-
-