Top Description Fields Constructors Methods
io.micrometer.context

public Class ContextRegistry

extends Object
Class Inheritance
Imports
java.util.Collections, .List, .ServiceLoader, java.util.concurrent.CopyOnWriteArrayList, java.util.function.Consumer, .Supplier

Registry that provides access to, instances of 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.

Author
Rossen Stoyanchev
Since
1.0.0

Field Summary

Modifier and TypeField and Description
private final List<ContextAccessor<?, ?>>
private static final ContextRegistry
private final List<ContextAccessor<?, ?>>
private final List<ThreadLocalAccessor<?>>
private final List<ThreadLocalAccessor<?>>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public ContextAccessor<?, ?>
getContextAccessorForRead(Object
the context to read from
context
)

Find a ContextAccessor that can read the given context.

public ContextAccessor<?, ?>
getContextAccessorForWrite(Object
the context to write to
context
)

Return a ContextAccessor that can write the given context.

public List<ContextAccessor<?, ?>>
getContextAccessors()

Return a read-only list of registered ContextAccessor's.

public static ContextRegistry
getInstance()

Return a global ContextRegistry instance.

public List<ThreadLocalAccessor<?>>
getThreadLocalAccessors()

Return a read-only list of registered ThreadLocalAccessor's.

public ContextRegistry
loadContextAccessors()

Load ContextAccessor implementations through the ServiceLoader mechanism.

public ContextRegistry
loadThreadLocalAccessors()

Load ThreadLocalAccessor implementations through the ServiceLoader mechanism.

public ContextRegistry
public <
the type of value stored in the ThreadLocal
V
>
ContextRegistry

Returns:

the same registry instance
registerThreadLocalAccessor
(String
the key to associate with the ThreadLocal value
key
,
ThreadLocal<V>
the underlying ThreadLocal
threadLocal
)

Register a ThreadLocalAccessor for the given ThreadLocal.

public <
the type of value stored in the ThreadLocal
V
>
ContextRegistry

Returns:

the same registry instance
registerThreadLocalAccessor
(String
the key to associate with the ThreadLocal value
key
,
Supplier<V>
callback to use for getting the value
getSupplier
,
Consumer<V>
callback to use for setting the value
setConsumer
,
Runnable
callback to use for resetting the value
resetTask
)

Register a ThreadLocalAccessor from callbacks.

public ContextRegistry

Returns:

the same registry instance
registerThreadLocalAccessor
(ThreadLocalAccessor<?>
the accessor to register
accessor
)

Register a ThreadLocalAccessor.

public boolean

Returns:

true when accessor got successfully removed
removeContextAccessor
(ContextAccessor<?, ?>
accessor instance to remove
accessorToRemove
)

Removes a registered ContextAccessor.

public boolean

Returns:

true when accessor got successfully removed
removeThreadLocalAccessor
(String
under which the accessor got registered
key
)

Removes a ThreadLocalAccessor.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

contextAccessorsback to summary
private final List<ContextAccessor<?, ?>> contextAccessors
instanceback to summary
private static final ContextRegistry instance
readOnlyContextAccessorsback to summary
private final List<ContextAccessor<?, ?>> readOnlyContextAccessors
readOnlyThreadLocalAccessorsback to summary
private final List<ThreadLocalAccessor<?>> readOnlyThreadLocalAccessors
threadLocalAccessorsback to summary
private final List<ThreadLocalAccessor<?>> threadLocalAccessors

Constructor Detail

ContextRegistryback to summary
public ContextRegistry()

Method Detail

getContextAccessorForReadback to summary
public ContextAccessor<?, ?> getContextAccessorForRead(Object context)

Find a ContextAccessor that can read the given context.

Parameters
context:Object

the context to read from

Exceptions
IllegalStateException:
if no match is found
getContextAccessorForWriteback to summary
public ContextAccessor<?, ?> getContextAccessorForWrite(Object context)

Return a ContextAccessor that can write the given context.

Parameters
context:Object

the context to write to

Exceptions
IllegalStateException:
if no match is found
getContextAccessorsback to summary
public List<ContextAccessor<?, ?>> getContextAccessors()

Return a read-only list of registered ContextAccessor's.

getInstanceback to summary
public static ContextRegistry getInstance()

Return a global ContextRegistry 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.

getThreadLocalAccessorsback to summary
public List<ThreadLocalAccessor<?>> getThreadLocalAccessors()

Return a read-only list of registered ThreadLocalAccessor's.

loadContextAccessorsback to summary
public ContextRegistry loadContextAccessors()

Load ContextAccessor implementations through the ServiceLoader mechanism.

Note that existing registrations of the same ContextAccessor type, if any, are removed first.

loadThreadLocalAccessorsback to summary
public ContextRegistry loadThreadLocalAccessors()

Load ThreadLocalAccessor implementations through the ServiceLoader mechanism.

Note that existing registrations with the same key, if any, are removed first.

registerContextAccessorback to summary
public ContextRegistry registerContextAccessor(ContextAccessor<?, ?> accessor)

Register a ContextAccessor. If there is an existing registration of another ContextAccessor that can work with its declared types, an exception is thrown.

registerThreadLocalAccessorback to summary
public <V> ContextRegistry registerThreadLocalAccessor(String key, ThreadLocal<V> threadLocal)

Register a ThreadLocalAccessor for the given ThreadLocal.

Parameters
<V>
the type of value stored in the ThreadLocal
key:String

the key to associate with the ThreadLocal value

threadLocal:ThreadLocal<V>

the underlying ThreadLocal

Returns:ContextRegistry

the same registry instance

registerThreadLocalAccessorback to summary
public <V> ContextRegistry registerThreadLocalAccessor(String key, Supplier<V> getSupplier, Consumer<V> setConsumer, Runnable resetTask)

Register a ThreadLocalAccessor from callbacks.

Parameters
<V>
the type of value stored in the ThreadLocal
key:String

the key to associate with the ThreadLocal value

getSupplier:Supplier<V>

callback to use for getting the value

setConsumer:Consumer<V>

callback to use for setting the value

resetTask:Runnable

callback to use for resetting the value

Returns:ContextRegistry

the same registry instance

registerThreadLocalAccessorback to summary
public ContextRegistry registerThreadLocalAccessor(ThreadLocalAccessor<?> accessor)

Register a ThreadLocalAccessor. If there is an existing registration with the same key, it is removed first.

Parameters
accessor:ThreadLocalAccessor<?>

the accessor to register

Returns:ContextRegistry

the same registry instance

removeContextAccessorback to summary
public boolean removeContextAccessor(ContextAccessor<?, ?> accessorToRemove)

Removes a registered ContextAccessor.

Parameters
accessorToRemove:ContextAccessor<?, ?>

accessor instance to remove

Returns:boolean

true when accessor got successfully removed

removeThreadLocalAccessorback to summary
public boolean removeThreadLocalAccessor(String key)

Removes a ThreadLocalAccessor.

Parameters
key:String

under which the accessor got registered

Returns:boolean

true when accessor got successfully removed

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override