Interface ThreadContext

All Known Implementing Classes:
DefaultThreadContext

public interface ThreadContext
This class represents the context of a thread, allowing to access and share information transparently across threads. A typical use case for a thread context is to store information about the current request in a servlet container.
  • Method Details

    • getPropertyValue

      @Nonnull <T> io.vavr.control.Try<T> getPropertyValue(@Nonnull String name)
      Retrieves a Try of the property's value for the given name. The Try is considered to fail in case of exceptions or if the property does not exist. Implementations have to ensure that this method is thread-safe.
      Type Parameters:
      T - The generic value type.
      Parameters:
      name - The name of the property.
      Returns:
      The value wrapped in a Try.
      Throws:
      ClassCastException - If the property cannot be cast to the desired type.
    • setPropertyIfAbsent

      void setPropertyIfAbsent(@Nonnull String name, @Nonnull Property<?> value) throws ThreadContextPropertyException
      Sets a value for the property for the given name, if it has not been set before. Implementations have to ensure that this method is thread-safe.
      Parameters:
      name - The name of the property.
      value - A Property.
      Throws:
      ThreadContextPropertyException - If there is an issue while setting the property.
    • setPropertyIfAbsent

      default void setPropertyIfAbsent(@Nonnull String name, @Nonnull Callable<Property<?>> valueGenerator) throws ThreadContextPropertyException
      Sets a value for the property for the given name, if it has not been set before. Implementations have to ensure that this method is thread-safe.
      Parameters:
      name - The name of the property.
      valueGenerator - A Callable that returns the Property to set
      Throws:
      ThreadContextPropertyException - If there is an issue while setting the property or executing the valueGenerator.
    • setProperty

      default void setProperty(@Nonnull String name, @Nonnull Property<?> value) throws ThreadContextPropertyException
      Set a value for the property for the given name, independent whether it has been set before. Implementations have to ensure that this method is thread-safe.
      Parameters:
      name - The name of the property.
      value - A Property.
      Throws:
      ThreadContextPropertyException - If there is an issue while setting the property.
    • removeProperty

      @Nonnull <T> io.vavr.control.Option<Property<T>> removeProperty(@Nonnull String name) throws ClassCastException
      Removes the property with the given name.

      Caution: Implementations may not be thread-safe!

      Type Parameters:
      T - The generic value type.
      Parameters:
      name - The name of the property.
      Returns:
      A Option holding the removed value, or Option.none() if the property did not exist before.
      Throws:
      ClassCastException - If the property cannot be cast to the desired type.
    • containsProperty

      boolean containsProperty(@Nonnull String name)
      Check whether a property with the given name is present and readable.

      Caution: Implementations may not be thread-safe!

      Parameters:
      name - The name of the property.
      Returns:
      A boolean indicating whether the property does exist and is readable.
    • duplicate

      @Nonnull default ThreadContext duplicate()
      Create a new ThreadContext containing all properties of this context. Both the new and the existing context will point to the same property objects. This is useful for passing on a context to a new thread, without the two contexts interfering with each other.

      Caution: Implementations may not be thread-safe!

      Returns:
      A new instance of ThreadContext.
      Throws:
      ThreadContextAccessException - If the current implementation does not support the copy operation.