public class ThreadContext
extends java.lang.Object
Map-like functions to keep track of key/value pairs for application threads. Keys are Strings, with values of any arbitrary object. Optionally keeps a log framework's MDC weakly in sync with changes -- that is, there is no atomicity guarantee so it's plausible that this class' context information and the logger's will not be in a consistent state; however, log4j and logback's MDC expose data only to the current thread via a thread-local so this is unlikely to be a problem in practice.
Most methods operate on the context of the calling thread; only
forThread(Thread)
allows cross-thread information retrieval.
Modifier and Type | Class and Description |
---|---|
static class |
ThreadContext.Log4jMdcBridge |
static interface |
ThreadContext.MdcBridge |
static class |
ThreadContext.NullMdcBridge |
static class |
ThreadContext.Slf4jMDCBridge |
Modifier and Type | Field and Description |
---|---|
private ThreadContext.MdcBridge |
mdcBridge |
private static com.google.common.cache.CacheLoader<java.lang.Thread,java.util.Map<java.lang.String,java.lang.Object>> |
NEW_CONTEXT_CREATOR |
private com.google.common.cache.LoadingCache<java.lang.Thread,java.util.Map<java.lang.String,java.lang.Object>> |
PER_THREAD_CONTEXTS |
Constructor and Description |
---|
ThreadContext() |
ThreadContext(ThreadContext.MdcBridge mdcBridge) |
Modifier and Type | Method and Description |
---|---|
java.util.Collection<java.lang.String> |
allKeys() |
void |
clear()
Clears all values for the current thread.
|
java.util.Map<java.lang.String,java.lang.Object> |
forThread(java.lang.Thread t)
Retrieves a copy of the thread context for the given thread
|
java.lang.Object |
get(java.lang.String key)
Retrieves the value corresponding to the supplied key for the current thread (null if no such
value exists)
|
java.lang.Object |
getForThread(java.lang.Thread thread,
java.lang.String key) |
static ThreadContext |
newMDCEnabledContext()
Factory method that creates a new ThreadContext initialized to also update Log4j's MDC.
|
static ThreadContext |
newSLF4JEnabledContext()
Factory method that creates a new ThreadContext initialised to also update SLF4J's MDC
|
void |
put(java.lang.String key,
java.lang.Object value)
Adds the given key/value pair to the current thread's context, and updates
ThreadContext.MdcBridge with
same. |
void |
remove(java.lang.String key)
Removes the given key from the current thread's context and
ThreadContext.MdcBridge . |
private static final com.google.common.cache.CacheLoader<java.lang.Thread,java.util.Map<java.lang.String,java.lang.Object>> NEW_CONTEXT_CREATOR
private final com.google.common.cache.LoadingCache<java.lang.Thread,java.util.Map<java.lang.String,java.lang.Object>> PER_THREAD_CONTEXTS
private volatile ThreadContext.MdcBridge mdcBridge
public ThreadContext()
public ThreadContext(ThreadContext.MdcBridge mdcBridge)
public void put(java.lang.String key, java.lang.Object value)
ThreadContext.MdcBridge
with
same.public void remove(java.lang.String key)
ThreadContext.MdcBridge
.public java.lang.Object get(java.lang.String key)
public void clear()
public java.util.Map<java.lang.String,java.lang.Object> forThread(java.lang.Thread t)
public java.util.Collection<java.lang.String> allKeys()
public java.lang.Object getForThread(java.lang.Thread thread, java.lang.String key)
public static ThreadContext newMDCEnabledContext()
public static ThreadContext newSLF4JEnabledContext()