@ParametersAreNonnullByDefault public class CallContext extends Object
It provides access to different sub-contexts via get(Class)
. Also, it provides acces to the mapped
diagnostic context (MDC). This can be filled by various parts of the framework (like which request-uri is
currently being processed, which user is currently active etc.) and will be attached to each error. Also, each
context comes with a new "flow-id". This can be used to trace an execution across different threads and even
across different cluster nodes.
Tasks which fork async subtasks will automatically pass on their current context. Therefore essential information can be passed along, without having to provide a method parameter for each value. Since sub-contexts can be of any type, this concept can be enhanced by additional frameworks or application programs.
Modifier and Type | Field and Description |
---|---|
static String |
MDC_FLOW
Name of the flow variable in the MDC.
|
static String |
MDC_PARENT
Name of the parent context in the MDC
|
Constructor and Description |
---|
CallContext() |
Modifier and Type | Method and Description |
---|---|
void |
addToMDC(String key,
String value)
Adds a value to the mapped diagnostic context.
|
void |
addToMDC(String key,
Supplier<String> value)
Adds a value to the mapped diagnostic context.
|
static void |
detach()
Detaches this CallContext from the current thread
|
void |
detachContext()
Detaches this context from the current thread.
|
CallContext |
fork()
Forks and creates a sub context.
|
<C extends SubContext> |
get(Class<C> contextType)
Returns or creates the sub context of the given type.
|
static Optional<CallContext> |
getContext(long threadId)
Returns the CallContext for the given thread or an empty optional if none is present.
|
static CallContext |
getCurrent()
Returns the context for the current thread.
|
static CallContext |
getCurrentIfAvailable()
Returns the context for the current thread.
|
String |
getFallbackLang()
Returns the current fallback language determined for the current thread.
|
static Counter |
getInteractionCounter()
Provides access to the interaction counter.
|
String |
getLang()
Returns the current language determined for the current thread.
|
List<Tuple<String,String>> |
getMDC()
Returns the current mapped diagnostic context (MDC).
|
Value |
getMDCValue(String key)
Returns the value of the named variable in the mdc.
|
static String |
getNodeName()
Returns the name of this computation node.
|
Watch |
getWatch()
Returns the Watch representing the execution time.
|
static CallContext |
initialize()
Creates a new CallContext for the given thread.
|
void |
removeFromMDC(String key)
Removes the value of the mdc for key.
|
<C extends SubContext> |
set(Class<C> contextType,
C instance)
Installs the given sub context.
|
static void |
setCurrent(CallContext context)
Sets the CallContext for the current thread.
|
void |
setFallbackLang(String fallbackLang)
Sets the current fallback language for the current thread.
|
void |
setLang(String lang)
Sets the current language for the current thread.
|
String |
toString() |
public static final String MDC_FLOW
public static final String MDC_PARENT
public static String getNodeName()
This is either the current host name or can be set via sirius.nodeName.
@Nonnull public static Optional<CallContext> getContext(long threadId)
threadId
- the id of the thread to fetch the CallContext for@Nullable public static CallContext getCurrentIfAvailable()
@Nonnull public static CallContext getCurrent()
If no context is available, a new one will be initialized.
public static Counter getInteractionCounter()
This counts all CallContexts which have been created and is used to provide rough system utilization metrics.
public static CallContext initialize()
Discards the current CallContext, if there was already one.
public CallContext fork()
All instantiated sub contexts are forked, the MDC is re-initialized.
SubContext.fork()
public static void setCurrent(CallContext context)
context
- the context to use for the current thread.public static void detach()
public void detachContext()
This will notify all sub contexts (SubContext
) that this context essentially ended.
public List<Tuple<String,String>> getMDC()
public Value getMDCValue(String key)
key
- the name of the variable to read.public Watch getWatch()
public void addToMDC(String key, @Nullable String value)
key
- the name of the value to addvalue
- the value to add to the mdc.public void addToMDC(String key, @Nullable Supplier<String> value)
key
- the name of the value to addvalue
- the supplier to add to the mdc. Will be evaluated one the MDC is used elsewhere.public void removeFromMDC(String key)
key
- the name of the value to remove.@Nonnull public <C extends SubContext> C get(Class<C> contextType)
The class of the sub context must provide a no-args constructor, as it will be instantiated if non existed.
C
- the type of the sub-contextcontextType
- the type of the sub-context to be returned.public <C extends SubContext> void set(Class<C> contextType, C instance)
This should only be used if required (e.g. in test environments to replace/mock objects). Otherwise a
call to get(Class)
will initialize the requested sub context.
C
- the type of the sub-contextcontextType
- the type of the context to setinstance
- the instance to setpublic String getLang()
@Nullable public String getFallbackLang()
public void setLang(@Nullable String lang)
If null or an empty string is passed in, the language will not be changed.
lang
- the two-letter language code for this thread.public void setFallbackLang(@Nullable String fallbackLang)
fallbackLang
- the two-letter language code for this thread.Copyright © 2018. All rights reserved.