Class SmallRyeThreadContext

java.lang.Object
io.smallrye.context.SmallRyeThreadContext
All Implemented Interfaces:
org.eclipse.microprofile.context.ThreadContext

public class SmallRyeThreadContext extends Object implements org.eclipse.microprofile.context.ThreadContext
  • Constructor Details

  • Method Details

    • withThreadContext

      public static CleanAutoCloseable withThreadContext(SmallRyeThreadContext threadContext)
      Updates the current @{link SmallRyeThreadContext} in use by the current thread, and returns an object suitable for use in try-with-resource to restore the previous value.
      Parameters:
      threadContext - the @{link SmallRyeThreadContext} to use
      Returns:
      an object suitable for use in try-with-resource to restore the previous value.
    • withThreadContext

      public static void withThreadContext(SmallRyeThreadContext threadContext, Runnable f)
      Invokes the given @{link Runnable} with the current @{link SmallRyeThreadContext} updated to the given value for the current thread.
      Parameters:
      threadContext - the @{link SmallRyeThreadContext} to use
      f - the @{link Runnable} to invoke
    • withThreadContext

      public static <T> T withThreadContext(SmallRyeThreadContext threadContext, Supplier<T> f)
      Invokes the given @{link Supplier} with the current @{link SmallRyeThreadContext} updated to the given value for the current thread.
      Type Parameters:
      T - The type of @{link Supplier} to return
      Parameters:
      threadContext - the @{link SmallRyeThreadContext} to use
      f - the @{link Supplier} to invoke
      Returns:
      The value returned by the @{link Supplier}
    • getCurrentThreadContextOrDefaultContexts

      public static SmallRyeThreadContext getCurrentThreadContextOrDefaultContexts()
      Returns the current thread's @{link SmallRyeThreadContext} if set, or a @{link SmallRyeThreadContext} with default contexts, possibly configured via MP Config.
      Returns:
      the current thread's @{link SmallRyeThreadContext} if set, or a @{link SmallRyeThreadContext} with default contexts, possibly configured via MP Config.
    • getCurrentThreadContextOrPropagatedContexts

      public static SmallRyeThreadContext getCurrentThreadContextOrPropagatedContexts()
      Returns the current thread's @{link SmallRyeThreadContext} if set, or a @{link SmallRyeThreadContext} which propagates all contexts.
      Returns:
      the current thread's @{link SmallRyeThreadContext} if set, or a @{link SmallRyeThreadContext} which propagates all contexts.
    • getCurrentThreadContextOrClearedContexts

      public static SmallRyeThreadContext getCurrentThreadContextOrClearedContexts()
      Returns the current thread's @{link SmallRyeThreadContext} if set, or a @{link SmallRyeThreadContext} which clears all contexts.
      Returns:
      the current thread's @{link SmallRyeThreadContext} if set, or a @{link SmallRyeThreadContext} which clears all contexts.
    • getCurrentThreadContext

      public static SmallRyeThreadContext getCurrentThreadContext(SmallRyeThreadContext defaultValue)
      Returns the current thread's @{link SmallRyeThreadContext} if set, or the given @{link SmallRyeThreadContext} default value.
      Parameters:
      defaultValue - the default value to use
      Returns:
      the current thread's @{link SmallRyeThreadContext} if set, or the given @{link SmallRyeThreadContext} default value.
    • getCurrentThreadContext

      public static SmallRyeThreadContext getCurrentThreadContext()
      Returns the current thread's @{link SmallRyeThreadContext} if set, or null.
      Returns:
      the current thread's @{link SmallRyeThreadContext} if set, or null.
    • getPlan

      public ThreadContextProviderPlan getPlan()
    • getDefaultExecutor

      public ExecutorService getDefaultExecutor()
    • isEmpty

      public boolean isEmpty()
      Returns true if this thread context has no context to propagate nor clear, and so will not contextualise anything.
      Returns:
      true if this thread context has no context to propagate nor clear
    • isContextualized

      public boolean isContextualized(Object lambda)
      Returns true if the given lambda instance is already contextualized
      Parameters:
      lambda - the lambda to test
      Returns:
      true if the given lambda instance is already contextualized
    • builder

      public static SmallRyeThreadContext.Builder builder()
    • withContextCapture

      public <T> CompletableFuture<T> withContextCapture(CompletableFuture<T> future)

      Returns a new CompletableFuture that is completed by the completion of the specified stage.

      The new completable future will use the same default executor as this ThreadContext, which can be a ManagedExecutor if this ThreadContext was obtained by SmallRyeManagedExecutor.getThreadContext() or the default executor service as set by SmallRyeContextManager.Builder.withDefaultExecutorService(ExecutorService), or otherwise have no default executor. If this thread context has no default executor, the new stage and all dependent stages created from it, and so forth, have no default asynchronous execution facility and must raise UnsupportedOperationException for all *Async methods that do not specify an executor. For example, thenRunAsync(Runnable).

      When dependent stages are created from the new completable future, thread context is captured and/or cleared as described in the documentation of the ManagedExecutor class, except that this ThreadContext instance takes the place of the default asynchronous execution facility in supplying the configuration of cleared/propagated context types. This guarantees that the action performed by each stage always runs under the thread context of the code that creates the stage, unless the user explicitly overrides by supplying a pre-contextualized action.

      Invocation of this method does not impact thread context propagation for the supplied completable future or any dependent stages created from it, other than the new dependent completable future that is created by this method.

      Specified by:
      withContextCapture in interface org.eclipse.microprofile.context.ThreadContext
      Type Parameters:
      T - completable future result type.
      Parameters:
      future - a completable future whose completion triggers completion of the new completable future that is created by this method.
      Returns:
      the new completable future.
    • withContextCapture

      public <T> CompletableFuture<T> withContextCapture(CompletableFuture<T> future, Executor executor, int flags)
    • withContextCapture

      public <T> CompletionStage<T> withContextCapture(CompletionStage<T> stage)

      Returns a new CompletionStage that is completed by the completion of the specified stage.

      The new completion stage will use the same default executor as this ThreadContext, which can be a ManagedExecutor if this ThreadContext was obtained by SmallRyeManagedExecutor.getThreadContext() or the default executor service as set by SmallRyeContextManager.Builder.withDefaultExecutorService(ExecutorService), or otherwise have no default executor. If this thread context has no default executor, the new stage and all dependent stages created from it, and so forth, and/or cleared as described in the documentation of the ManagedExecutor class, except that this ThreadContext instance takes the place of the default asynchronous execution facility in supplying the configuration of cleared/propagated context types. This guarantees that the action performed by each stage always runs under the thread context of the code that creates the stage, unless the user explicitly overrides by supplying a pre-contextualized action.

      Invocation of this method does not impact thread context propagation for the supplied stage or any dependent stages created from it, other than the new dependent completion stage that is created by this method.

      Specified by:
      withContextCapture in interface org.eclipse.microprofile.context.ThreadContext
      Type Parameters:
      T - completion stage result type.
      Parameters:
      stage - a completion stage whose completion triggers completion of the new stage that is created by this method.
      Returns:
      the new completion stage.
    • withContextCapture

      public <T> CompletionStage<T> withContextCapture(CompletionStage<T> stage, Executor executor)
    • completedFuture

      public <U> CompletableFuture<U> completedFuture(U value)

      Returns a new CompletableFuture that is already completed with the specified value.

      The new completable future will use the same default executor as this ThreadContext, which can be a ManagedExecutor if this ThreadContext was obtained by SmallRyeManagedExecutor.getThreadContext() or the default executor service as set by SmallRyeContextManager.Builder.withDefaultExecutorService(ExecutorService), or otherwise have no default executor.

      Type Parameters:
      U - result type of the completable future.
      Parameters:
      value - result with which the completable future is completed.
      Returns:
      the new completable future.
    • completedStage

      public <U> CompletionStage<U> completedStage(U value)

      Returns a new CompletionStage that is already completed with the specified value.

      The new completion stage will use the same default executor as this ThreadContext, which can be a ManagedExecutor if this ThreadContext was obtained by SmallRyeManagedExecutor.getThreadContext() or the default executor service as set by SmallRyeContextManager.Builder.withDefaultExecutorService(ExecutorService), or otherwise have no default executor.

      Type Parameters:
      U - result type of the completion stage.
      Parameters:
      value - result with which the completable future is completed.
      Returns:
      the new completion stage.
    • failedFuture

      public <U> CompletableFuture<U> failedFuture(Throwable ex)

      Returns a new CompletableFuture that is already exceptionally completed with the specified Throwable.

      The new completable future will use the same default executor as this ThreadContext, which can be a ManagedExecutor if this ThreadContext was obtained by SmallRyeManagedExecutor.getThreadContext() or the default executor service as set by SmallRyeContextManager.Builder.withDefaultExecutorService(ExecutorService), or otherwise have no default executor.

      Type Parameters:
      U - result type of the completable future.
      Parameters:
      ex - exception or error with which the completable future is completed.
      Returns:
      the new completable future.
    • failedStage

      public <U> CompletionStage<U> failedStage(Throwable ex)

      Returns a new CompletionStage that is already exceptionally completed with the specified Throwable.

      The new completion stage will use the same default executor as this ThreadContext, which can be a ManagedExecutor if this ThreadContext was obtained by SmallRyeManagedExecutor.getThreadContext() or the default executor service as set by SmallRyeContextManager.Builder.withDefaultExecutorService(ExecutorService), or otherwise have no default executor.

      Type Parameters:
      U - result type of the completion stage.
      Parameters:
      ex - exception or error with which the stage is completed.
      Returns:
      the new completion stage.
    • newIncompleteFuture

      public <U> CompletableFuture<U> newIncompleteFuture()

      Returns a new incomplete CompletableFuture.

      The new completable future will use the same default executor as this ThreadContext, which can be a ManagedExecutor if this ThreadContext was obtained by SmallRyeManagedExecutor.getThreadContext() or the default executor service as set by SmallRyeContextManager.Builder.withDefaultExecutorService(ExecutorService), or otherwise have no default executor.

      Type Parameters:
      U - result type of the completion stage.
      Returns:
      the new completion stage.
    • copy

      public <T> CompletableFuture<T> copy(CompletableFuture<T> stage)

      Returns a new CompletableFuture that is completed by the completion of the specified stage.

      The new completable future is backed by the ManagedExecutor upon which copy is invoked, which serves as the default asynchronous execution facility for the new stage and all dependent stages created from it, and so forth.

      When dependent stages are created from the new completable future, thread context is captured and/or cleared by the ManagedExecutor. This guarantees that the action performed by each stage always runs under the thread context of the code that creates the stage, unless the user explicitly overrides by supplying a pre-contextualized action.

      Invocation of this method does not impact thread context propagation for the supplied completable future or any dependent stages created from it, other than the new dependent completable future that is created by this method.

      Type Parameters:
      T - completable future result type.
      Parameters:
      stage - a completable future whose completion triggers completion of the new completable future that is created by this method.
      Returns:
      the new completable future.
    • copy

      public <T> CompletionStage<T> copy(CompletionStage<T> stage)

      Returns a new CompletionStage that is completed by the completion of the specified stage.

      The new completable future is backed by the ManagedExecutor upon which copy is invoked, which serves as the default asynchronous execution facility for the new stage and all dependent stages created from it, and so forth.

      When dependent stages are created from the new completable future, thread context is captured and/or cleared by the ManagedExecutor. This guarantees that the action performed by each stage always runs under the thread context of the code that creates the stage, unless the user explicitly overrides by supplying a pre-contextualized action.

      Invocation of this method does not impact thread context propagation for the supplied stage or any dependent stages created from it, other than the new dependent completion stage that is created by this method.

      Type Parameters:
      T - completion stage result type.
      Parameters:
      stage - a completion stage whose completion triggers completion of the new stage that is created by this method.
      Returns:
      the new completion stage.
    • currentContextExecutor

      public Executor currentContextExecutor()
      Specified by:
      currentContextExecutor in interface org.eclipse.microprofile.context.ThreadContext
    • contextualConsumer

      public <T, U> BiConsumer<T,U> contextualConsumer(BiConsumer<T,U> consumer)
      Specified by:
      contextualConsumer in interface org.eclipse.microprofile.context.ThreadContext
    • contextualFunction

      public <T, U, R> BiFunction<T,U,R> contextualFunction(BiFunction<T,U,R> function)
      Specified by:
      contextualFunction in interface org.eclipse.microprofile.context.ThreadContext
    • contextualCallable

      public <R> Callable<R> contextualCallable(Callable<R> callable)
      Specified by:
      contextualCallable in interface org.eclipse.microprofile.context.ThreadContext
    • contextualConsumer

      public <T> Consumer<T> contextualConsumer(Consumer<T> consumer)
      Specified by:
      contextualConsumer in interface org.eclipse.microprofile.context.ThreadContext
    • contextualFunction

      public <T, R> Function<T,R> contextualFunction(Function<T,R> function)
      Specified by:
      contextualFunction in interface org.eclipse.microprofile.context.ThreadContext
    • contextualRunnable

      public Runnable contextualRunnable(Runnable runnable)
      Specified by:
      contextualRunnable in interface org.eclipse.microprofile.context.ThreadContext
    • contextualSupplier

      public <R> Supplier<R> contextualSupplier(Supplier<R> supplier)
      Specified by:
      contextualSupplier in interface org.eclipse.microprofile.context.ThreadContext
    • toString

      public String toString()
      Overrides:
      toString in class Object