Class ContextRegistry
ContextAccessor
and
ThreadLocalAccessor
.
A static instance is available via getInstance()
. It is intended to be
initialized on startup, and to be aware of all available accessors, as many as
possible. The means to control what context gets propagated is in
ContextSnapshot
, which filters context values by key.
- Since:
- 1.0.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionContextAccessor<?,
?> getContextAccessorForRead
(Object context) Find aContextAccessor
that can read the given context.ContextAccessor<?,
?> getContextAccessorForWrite
(Object context) Return aContextAccessor
that can write the given context.List<ContextAccessor<?,
?>> Return a read-only list of registeredContextAccessor
's.static ContextRegistry
Return a globalContextRegistry
instance.Return a read-only list of registeredThreadLocalAccessor
's.LoadContextAccessor
implementations through theServiceLoader
mechanism.LoadThreadLocalAccessor
implementations through theServiceLoader
mechanism.registerContextAccessor
(ContextAccessor<?, ?> accessor) Register aContextAccessor
.registerThreadLocalAccessor
(ThreadLocalAccessor<?> accessor) Register aThreadLocalAccessor
.<V> ContextRegistry
registerThreadLocalAccessor
(String key, ThreadLocal<V> threadLocal) Register aThreadLocalAccessor
for the givenThreadLocal
.<V> ContextRegistry
registerThreadLocalAccessor
(String key, Supplier<V> getSupplier, Consumer<V> setConsumer, Runnable resetTask) Register aThreadLocalAccessor
from callbacks.boolean
removeContextAccessor
(ContextAccessor<?, ?> accessorToRemove) Removes a registeredContextAccessor
.boolean
Removes aThreadLocalAccessor
.toString()
-
Constructor Details
-
ContextRegistry
public ContextRegistry()
-
-
Method Details
-
registerContextAccessor
Register aContextAccessor
. If there is an existing registration of anotherContextAccessor
that can work with its declared types, an exception is thrown. -
registerThreadLocalAccessor
Register aThreadLocalAccessor
for the givenThreadLocal
.- Type Parameters:
V
- the type of value stored in the ThreadLocal- Parameters:
key
- thekey
to associate with the ThreadLocal valuethreadLocal
- the underlyingThreadLocal
- Returns:
- the same registry instance
-
registerThreadLocalAccessor
public <V> ContextRegistry registerThreadLocalAccessor(String key, Supplier<V> getSupplier, Consumer<V> setConsumer, Runnable resetTask) Register aThreadLocalAccessor
from callbacks.- Type Parameters:
V
- the type of value stored in the ThreadLocal- Parameters:
key
- thekey
to associate with the ThreadLocal valuegetSupplier
- callback to use for getting the valuesetConsumer
- callback to use for setting the valueresetTask
- callback to use for resetting the value- Returns:
- the same registry instance
-
registerThreadLocalAccessor
Register aThreadLocalAccessor
. If there is an existing registration with the samekey
, it is removed first.- Parameters:
accessor
- the accessor to register- Returns:
- the same registry instance
-
removeThreadLocalAccessor
Removes aThreadLocalAccessor
.- Parameters:
key
- under which the accessor got registered- Returns:
true
when accessor got successfully removed
-
removeContextAccessor
Removes a registeredContextAccessor
.- Parameters:
accessorToRemove
- accessor instance to remove- Returns:
true
when accessor got successfully removed
-
loadContextAccessors
LoadContextAccessor
implementations through theServiceLoader
mechanism.Note that existing registrations of the same
ContextAccessor
type, if any, are removed first. -
loadThreadLocalAccessors
LoadThreadLocalAccessor
implementations through theServiceLoader
mechanism.Note that existing registrations with the same
key
, if any, are removed first. -
getContextAccessorForRead
Find aContextAccessor
that can read the given context.- Parameters:
context
- the context to read from- Throws:
IllegalStateException
- if no match is found
-
getContextAccessorForWrite
Return aContextAccessor
that can write the given context.- Parameters:
context
- the context to write to- Throws:
IllegalStateException
- if no match is found
-
getContextAccessors
Return a read-only list of registeredContextAccessor
's. -
getThreadLocalAccessors
Return a read-only list of registeredThreadLocalAccessor
's. -
toString
-
getInstance
Return a globalContextRegistry
instance.Note: The global instance should be initialized on startup to ensure it has the ability to propagate to and from different types of context throughout the application. The registry itself is not intended to as a mechanism to control what gets propagated. It is in
ContextSnapshot
where more fine-grained decisions can be made about which context values to propagate.
-