Package io.micrometer.context
Interface ThreadLocalAccessor<V>
- All Known Implementing Classes:
Slf4jThreadLocalAccessor
public interface ThreadLocalAccessor<V>
Contract to assist with setting and clearing a
ThreadLocal.- Since:
- 1.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescription@Nullable VgetValue()Return the currentThreadLocalvalue.key()The key to associate with the ThreadLocal value when saved within aContextSnapshot.default voidreset()Deprecated.default voidrestore()Called instead ofrestore(Object)when there was noThreadLocalvalue existing at the start of the scope.default voidRestore theThreadLocalat the end of aContextSnapshot.Scopeto the previous value it had before the start of the scope.default voidsetValue()Called instead ofsetValue(Object)in order to remove the currentThreadLocalvalue at the start of aContextSnapshot.Scope.voidSet theThreadLocalat the start of aContextSnapshot.Scopeto a value obtained from aContextSnapshotor 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
@Nullable V getValue()Return the currentThreadLocalvalue.This method is called in two scenarios:
- When capturing a
ContextSnapshot. Anullvalue is ignored and thekey()will not be present in the snapshot. - When setting
ThreadLocalvalues from aContextSnapshotor from a Context object (through aContextAccessor) to save existing values in order torestore(Object)them at the end of the scope. Anullvalue means theThreadLocalshould not be set and upon closing aContextSnapshot.Scope, therestore()variant is called.
- When capturing a
-
setValue
Set theThreadLocalat the start of aContextSnapshot.Scopeto a value obtained from aContextSnapshotor from a different type of context (through aContextAccessor).- Parameters:
value- the value to set, nevernullwhen called from aContextSnapshotimplementation, which is not allowed to store mappings tonull.
-
setValue
default void setValue()Called instead ofsetValue(Object)in order to remove the currentThreadLocalvalue 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 theThreadLocalvalue when settingThreadLocalvalues in case of missing mapping for akey()from aContextSnapshot, or a Context object (operated upon byContextAccessor). -
restore
Restore theThreadLocalat the end of aContextSnapshot.Scopeto the previous value it had before the start of the scope.- Parameters:
previousValue- previous value to set, nevernullwhen 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 noThreadLocalvalue 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.