Package io.micrometer.context
Interface ThreadLocalAccessor<V>
public interface ThreadLocalAccessor<V>
Contract to assist with setting and clearing a
ThreadLocal
.- Since:
- 1.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiongetValue()
Return the currentThreadLocal
value.key()
The key to associate with the ThreadLocal value when saved within aContextSnapshot
.default void
reset()
Deprecated.default void
restore()
Called instead ofrestore(Object)
when there was noThreadLocal
value existing at the start of the scope.default void
Restore theThreadLocal
at the end of aContextSnapshot.Scope
to the previous value it had before the start of the scope.default void
setValue()
Called instead ofsetValue(Object)
in order to remove the currentThreadLocal
value at the start of aContextSnapshot.Scope
.void
Set theThreadLocal
at the start of aContextSnapshot.Scope
to a value obtained from aContextSnapshot
or from a different type of context (through aContextAccessor
).
-
Method Details
-
key
Object key()The key to associate with the ThreadLocal value when saved within aContextSnapshot
. -
getValue
Return the currentThreadLocal
value.This method is called in two scenarios:
- When capturing a
ContextSnapshot
. Anull
value is ignored and thekey()
will not be present in the snapshot. - When setting
ThreadLocal
values from aContextSnapshot
or from a Context object (through aContextAccessor
) to save existing values in order torestore(Object)
them at the end of the scope. Anull
value means theThreadLocal
should not be set and upon closing aContextSnapshot.Scope
, therestore()
variant is called.
- When capturing a
-
setValue
Set theThreadLocal
at the start of aContextSnapshot.Scope
to a value obtained from aContextSnapshot
or from a different type of context (through aContextAccessor
).- Parameters:
value
- the value to set, nevernull
when called from aContextSnapshot
implementation, which is not allowed to store mappings tonull
.
-
setValue
default void setValue()Called instead ofsetValue(Object)
in order to remove the currentThreadLocal
value at the start of aContextSnapshot.Scope
.- Since:
- 1.0.3
-
reset
Deprecated.To be replaced by calls tosetValue()
(and/orrestore()
), which needs to be implemented when this implementation is removed.Remove theThreadLocal
value when settingThreadLocal
values in case of missing mapping for akey()
from aContextSnapshot
, or a Context object (operated upon byContextAccessor
). -
restore
Restore theThreadLocal
at the end of aContextSnapshot.Scope
to the previous value it had before the start of the scope.- Parameters:
previousValue
- previous value to set, nevernull
when called from aContextSnapshot
* implementation, which is not allowed to store mappings tonull
.- Since:
- 1.0.1
-
restore
default void restore()Called instead ofrestore(Object)
when there was noThreadLocal
value existing at the start of the scope.- Since:
- 1.0.3
- See Also:
-
setValue()
(and/orrestore()
), which needs to be implemented when this implementation is removed.