Class SmallRyeThreadContext

  • All Implemented Interfaces:
    org.eclipse.microprofile.context.ThreadContext

    public class SmallRyeThreadContext
    extends Object
    implements org.eclipse.microprofile.context.ThreadContext
    • Method Detail

      • 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
      • 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.
      • 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> 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.
      • 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.
      • 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