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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SmallRyeThreadContext.Builder
-
Constructor Summary
Constructors Constructor Description SmallRyeThreadContext(SmallRyeContextManager manager, String[] propagated, String[] unchanged, String[] cleared, String injectionPointName, ExecutorService defaultExecutor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SmallRyeThreadContext.Builder
builder()
<U> CompletableFuture<U>
completedFuture(U value)
Returns a new CompletableFuture that is already completed with the specified value.<U> CompletionStage<U>
completedStage(U value)
Returns a new CompletionStage that is already completed with the specified value.<R> Callable<R>
contextualCallable(Callable<R> callable)
<T,U>
BiConsumer<T,U>contextualConsumer(BiConsumer<T,U> consumer)
<T> Consumer<T>
contextualConsumer(Consumer<T> consumer)
<T,U,R>
BiFunction<T,U,R>contextualFunction(BiFunction<T,U,R> function)
<T,R>
Function<T,R>contextualFunction(Function<T,R> function)
Runnable
contextualRunnable(Runnable runnable)
<R> Supplier<R>
contextualSupplier(Supplier<R> supplier)
<T> CompletableFuture<T>
copy(CompletableFuture<T> stage)
Returns a newCompletableFuture
that is completed by the completion of the specified stage.<T> CompletionStage<T>
copy(CompletionStage<T> stage)
Returns a newCompletionStage
that is completed by the completion of the specified stage.Executor
currentContextExecutor()
<U> CompletableFuture<U>
failedFuture(Throwable ex)
Returns a new CompletableFuture that is already exceptionally completed with the specified Throwable.<U> CompletionStage<U>
failedStage(Throwable ex)
Returns a new CompletionStage that is already exceptionally completed with the specified Throwable.static SmallRyeThreadContext
getCurrentThreadContext()
Returns the current thread's @{link SmallRyeThreadContext} if set, or null.static SmallRyeThreadContext
getCurrentThreadContext(SmallRyeThreadContext defaultValue)
Returns the current thread's @{link SmallRyeThreadContext} if set, or the given @{link SmallRyeThreadContext} default value.static SmallRyeThreadContext
getCurrentThreadContextOrClearedContexts()
Returns the current thread's @{link SmallRyeThreadContext} if set, or a @{link SmallRyeThreadContext} which clears all contexts.static SmallRyeThreadContext
getCurrentThreadContextOrPropagatedContexts()
Returns the current thread's @{link SmallRyeThreadContext} if set, or a @{link SmallRyeThreadContext} which propagates all contexts.ExecutorService
getDefaultExecutor()
ThreadContextProviderPlan
getPlan()
<U> CompletableFuture<U>
newIncompleteFuture()
Returns a new incompleteCompletableFuture
.String
toString()
<T> CompletableFuture<T>
withContextCapture(CompletableFuture<T> future)
Returns a newCompletableFuture
that is completed by the completion of the specified stage.<T> CompletableFuture<T>
withContextCapture(CompletableFuture<T> future, Executor executor, int flags)
<T> CompletionStage<T>
withContextCapture(CompletionStage<T> stage)
Returns a newCompletionStage
that is completed by the completion of the specified stage.<T> CompletionStage<T>
withContextCapture(CompletionStage<T> stage, Executor executor)
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.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.
-
-
-
Constructor Detail
-
SmallRyeThreadContext
public SmallRyeThreadContext(SmallRyeContextManager manager, String[] propagated, String[] unchanged, String[] cleared, String injectionPointName, ExecutorService defaultExecutor)
-
-
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 usef
- 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.
-
getPlan
public ThreadContextProviderPlan getPlan()
-
getDefaultExecutor
public ExecutorService getDefaultExecutor()
-
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 bySmallRyeContextManager.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 raiseUnsupportedOperationException
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 interfaceorg.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 bySmallRyeContextManager.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 theManagedExecutor
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 interfaceorg.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 bySmallRyeContextManager.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 bySmallRyeContextManager.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 bySmallRyeContextManager.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 bySmallRyeContextManager.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 bySmallRyeContextManager.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 interfaceorg.eclipse.microprofile.context.ThreadContext
-
contextualConsumer
public <T,U> BiConsumer<T,U> contextualConsumer(BiConsumer<T,U> consumer)
- Specified by:
contextualConsumer
in interfaceorg.eclipse.microprofile.context.ThreadContext
-
contextualFunction
public <T,U,R> BiFunction<T,U,R> contextualFunction(BiFunction<T,U,R> function)
- Specified by:
contextualFunction
in interfaceorg.eclipse.microprofile.context.ThreadContext
-
contextualCallable
public <R> Callable<R> contextualCallable(Callable<R> callable)
- Specified by:
contextualCallable
in interfaceorg.eclipse.microprofile.context.ThreadContext
-
contextualConsumer
public <T> Consumer<T> contextualConsumer(Consumer<T> consumer)
- Specified by:
contextualConsumer
in interfaceorg.eclipse.microprofile.context.ThreadContext
-
contextualFunction
public <T,R> Function<T,R> contextualFunction(Function<T,R> function)
- Specified by:
contextualFunction
in interfaceorg.eclipse.microprofile.context.ThreadContext
-
contextualRunnable
public Runnable contextualRunnable(Runnable runnable)
- Specified by:
contextualRunnable
in interfaceorg.eclipse.microprofile.context.ThreadContext
-
contextualSupplier
public <R> Supplier<R> contextualSupplier(Supplier<R> supplier)
- Specified by:
contextualSupplier
in interfaceorg.eclipse.microprofile.context.ThreadContext
-
-