Interface ContextSnapshot


public interface ContextSnapshot
Holds values extracted from ThreadLocal and other types of context and exposes methods to propagate those values.

Use static factory methods on this interface to create a snapshot.

Since:
1.0.0
  • Method Details

    • updateContext

      <C> C updateContext(C context)
      Update the given context with all snapshot values.
      Type Parameters:
      C - the type of the target context
      Parameters:
      context - the context to write to
      Returns:
      a context, possibly a new instance, with the written values
    • updateContext

      <C> C updateContext(C context, Predicate<Object> keyPredicate)
      Variant of updateContext(Object) to update the given context with a subset of snapshot values.
      Type Parameters:
      C - the type of the target context
      Parameters:
      context - the context to write to
      keyPredicate - predicate for context value keys
      Returns:
      a context, possibly a new instance, with the written values
    • setThreadLocals

      ContextSnapshot.Scope setThreadLocals()
      Set ThreadLocal values from the snapshot.
      Returns:
      an object that can be used to reset ThreadLocal values at the end of the context scope, either removing them or restoring their previous values, if any.
    • setThreadLocals

      ContextSnapshot.Scope setThreadLocals(Predicate<Object> keyPredicate)
      Variant of setThreadLocals() with a predicate to select context values by key.
      Returns:
      an object that can be used to reset ThreadLocal values at the end of the context scope, either removing them or restoring their previous values, if any.
    • wrap

      default Runnable wrap(Runnable runnable)
      Return a new Runnable that sets ThreadLocal values from the snapshot around the invocation of the given Runnable.
      Parameters:
      runnable - the runnable to instrument
    • wrap

      default <T> Callable<T> wrap(Callable<T> callable)
      Return a new Callable that sets ThreadLocal values from the snapshot around the invocation of the given Callable.
      Type Parameters:
      T - the type of value produced by the Callable
      Parameters:
      callable - the callable to instrument
    • wrap

      default <T> Consumer<T> wrap(Consumer<T> consumer)
      Return a new Consumer that sets ThreadLocal values from the snapshot around the invocation of the given Consumer.
      Type Parameters:
      T - the type of value produced by the Callable
      Parameters:
      consumer - the callable to instrument
    • wrapExecutor

      default Executor wrapExecutor(Executor executor)
      Return a new Executor that sets ThreadLocal values from the snapshot around the invocation of any executed, Runnable.
      Parameters:
      executor - the executor to instrument
      See Also:
    • captureAll

      static ContextSnapshot captureAll(Object... contexts)
      Capture values from ThreadLocal and from other context objects using all accessors from the global ContextRegistry instance.
      Parameters:
      contexts - one more context objects to extract values from
      Returns:
      a snapshot with saved context values
    • captureAll

      static ContextSnapshot captureAll(ContextRegistry registry, Object... contexts)
      Capture values from ThreadLocal and from other context objects using all accessors from the global ContextRegistry instance.
      Parameters:
      registry - the registry to use
      contexts - one more context objects to extract values from
      Returns:
      a snapshot with saved context values
    • captureAllUsing

      static ContextSnapshot captureAllUsing(Predicate<Object> keyPredicate, ContextRegistry registry, Object... contexts)
      Variant of captureAll(Object...) with a predicate to filter context keys and with a specific ContextRegistry instance.
      Parameters:
      keyPredicate - predicate for context value keys
      registry - the registry with the accessors to use
      contexts - one more context objects to extract values from
      Returns:
      a snapshot with saved context values
    • captureFrom

      static ContextSnapshot captureFrom(Object context)
      Create a ContextSnapshot by reading values from the given context object.
      Parameters:
      context - the context to read values from
      Returns:
      the created ContextSnapshot
    • captureFrom

      static ContextSnapshot captureFrom(Object context, ContextRegistry registry)
      Create a ContextSnapshot by reading values from the given context object.
      Parameters:
      context - the context to read values from
      registry - the registry to use
      Returns:
      the created ContextSnapshot
    • captureFrom

      static ContextSnapshot captureFrom(Object context, Predicate<Object> keyPredicate, ContextRegistry registry)
      Create a ContextSnapshot by reading values from the given context object.
      Parameters:
      context - the context to read values from
      Returns:
      the created ContextSnapshot
    • setAllThreadLocalsFrom

      static ContextSnapshot.Scope setAllThreadLocalsFrom(Object sourceContext)
      Variant of setThreadLocalsFrom(Object, String...) that sets all ThreadLocal values for which there is a value in the given source context.
      Parameters:
      sourceContext - the source context to read values from
      Returns:
      an object that can be used to reset ThreadLocal values at the end of the context scope, either removing them or restoring their previous values, if any.
    • setAllThreadLocalsFrom

      static ContextSnapshot.Scope setAllThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry)
      Variant of setThreadLocalsFrom(Object, String...) that sets all ThreadLocal values for which there is a value in the given source context.
      Parameters:
      sourceContext - the source context to read values from
      contextRegistry - the registry with the accessors to use
      Returns:
      an object that can be used to reset ThreadLocal values at the end of the context scope, either removing them or restoring their previous values, if any.
    • setThreadLocalsFrom

      static ContextSnapshot.Scope setThreadLocalsFrom(Object sourceContext, String... keys)
      Read the values specified by from the given source context, and if found, use them to set ThreadLocal values. Essentially, a shortcut that bypasses the need to create of ContextSnapshot first via captureAll(Object...), followed by setThreadLocals().
      Parameters:
      sourceContext - the source context to read values from
      keys - the keys of the values to read (at least one key must be passed)
      Returns:
      an object that can be used to reset ThreadLocal values at the end of the context scope, either removing them or restoring their previous values, if any.
    • setThreadLocalsFrom

      static ContextSnapshot.Scope setThreadLocalsFrom(Object sourceContext, ContextRegistry contextRegistry, String... keys)
      Variant of setThreadLocalsFrom(Object, String...) with a specific ContextRegistry instead of the global instance.
      Parameters:
      sourceContext - the source context to read values from
      contextRegistry - the registry with the accessors to use
      keys - the keys of the values to read (at least one key must be passed)
      Returns:
      an object that can be used to reset ThreadLocal values at the end of the context scope, either removing them or restoring their previous values, if any.