Example usage:
ThreadContext threadContext = ThreadContext.builder()
.propagated(ThreadContext.SECURITY, ThreadContext.APPLICATION)
.cleared(ThreadContext.TRANSACTION)
.unchanged(ThreadContext.ALL_REMAINING)
.build();
...
CompletableFuture<Integer> stage2 = stage1.thenApply(threadContext.contextualFunction(function));
...
Future<Integer> future = executor.submit(threadContext.contextualCallable(callable));
This interface is intentionally kept compatible with ContextService, with the hope that its methods might one day be contributed to that specification.
| Modifier and Type | Class and Description |
|---|---|
| public static interface | ThreadContext.
Builder for |
| Modifier and Type | Field and Description |
|---|---|
| public static final String | ALL_REMAINING
Identifier for all available thread context types which are
not specified individually under |
| public static final String | APPLICATION
Identifier for application context. |
| public static final String | CDI
Identifier for CDI context. |
| public static final String[] | NONE
An empty array of thread context. |
| public static final String | SECURITY
Identifier for security context. |
| public static final String | TRANSACTION
Identifier for transaction context. |
| Modifier and Type | Method and Description |
|---|---|
| public static ThreadContext. | |
| public < callable result type. R> Callable | Returns: contextualized proxy instance that wraps execution of thecall method with context.instance to contextualize. callable)Wraps a |
| public < type of first parameter to consumer. T, type of second parameter to consumer. U> BiConsumer | Returns: contextualized proxy instance that wraps execution of theaccept method with context.instance to contextualize. consumer)Wraps a |
| public < type of parameter to consumer. T> Consumer | Returns: contextualized proxy instance that wraps execution of theaccept method with context.instance to contextualize. consumer)Wraps a |
| public < type of first parameter to function. T, type of second parameter to function. U, function result type. R> BiFunction | Returns: contextualized proxy instance that wraps execution of theapply method with context.instance to contextualize. function)Wraps a |
| public < type of parameter to function. T, function result type. R> Function | Returns: contextualized proxy instance that wraps execution of theapply method with context.instance to contextualize. function)Wraps a |
| public Runnable | Returns: contextualized proxy instance that wraps execution of therun method with context.instance to contextualize. runnable)Wraps a |
| public < supplier result type. R> Supplier | Returns: contextualized proxy instance that wraps execution of thesupply method with context.instance to contextualize. supplier)Wraps a |
| public Executor | Returns: an executor that wraps theexecute method with context.Creates an |
| public < completable future result type. T> CompletableFuture | Returns: the new completable future.a completable future whose completion triggers completion of the new completable
future that is created by this method. stage)Returns a new |
| public < completion stage result type. T> CompletionStage | Returns: the new completion stage.a completion stage whose completion triggers completion of the new stage
that is created by this method. stage)Returns a new |
| ALL_REMAINING | back to summary |
|---|---|
| public static final String ALL_REMAINING Identifier for all available thread context types which are
not specified individually under When using this constant, be aware that bringing in a new context provider or updating levels of an existing context provider might change the set of available thread context types. | |
| APPLICATION | back to summary |
|---|---|
| public static final String APPLICATION Identifier for application context. Application context controls the application component that is associated with a thread. For Jakarta/Java EE applications, application context includes the thread context class loader as well as the java:comp, java:module, and java:app name spaces of the application. An empty/default application context means that the thread is not associated with any application. | |
| CDI | back to summary |
|---|---|
| public static final String CDI Identifier for CDI context. CDI context controls the availability of CDI
scopes. An empty/default CDI context means that the thread does not have
access to the scope of the session, request, and so forth that created the
contextualized action.
For example, consider the following
CDI context propagation includes request, session and conversation contexts.
When CDI context is propagated, all of the above mentioned contexts that are
currently active will be available to the contextualized task with preserved
state.
If CDI context is 'cleared', currently active contexts will still be
available to the contextualized task, but their state will be erased.
If CDI context is 'unchanged', access to CDI bean's contextual state
will be non-deterministic. Namely, context may be missing, or context
from a different task may be applied instead. This option is discouraged,
and only should be used if CDI context is not used in an application.
| |
| NONE | back to summary |
|---|---|
| public static final String[] NONE An empty array of thread context. This is provided as a convenience for code that wishes to be more explicit.
For example, you can specify When using MicroProfile Config to specify defaults, the value
mp.context.ThreadContext.unchanged=Noneor mp.context.ManagedExecutor.propagated=None | |
| SECURITY | back to summary |
|---|---|
| public static final String SECURITY Identifier for security context. Security context controls the credentials that are associated with the thread. An empty/default security context means that the thread is unauthenticated. | |
| TRANSACTION | back to summary |
|---|---|
| public static final String TRANSACTION Identifier for transaction context. Transaction context controls the active transaction scope that is associated with the thread. Implementations are not expected to propagate transaction context across threads. Instead, the concept of transaction context is provided for its cleared context, which means the active transaction on the thread is suspended such that a new transaction can be started if so desired. In most cases, the most desirable behavior will be to leave transaction context defaulted to cleared (suspended), in order to prevent dependent actions and tasks from accidentally enlisting in transactions that are on the threads where they happen to run. | |
| builder | back to summary |
|---|---|
| public static ThreadContext. Creates a new
| |
| contextualCallable | back to summary |
|---|---|
| public <R> Callable Wraps a When
| |
| contextualConsumer | back to summary |
|---|---|
| public <T, U> BiConsumer Wraps a When
| |
| contextualConsumer | back to summary |
|---|---|
| public <T> Consumer Wraps a When
| |
| contextualFunction | back to summary |
|---|---|
| public <T, U, R> BiFunction Wraps a When
| |
| contextualFunction | back to summary |
|---|---|
| public <T, R> Function Wraps a When
| |
| contextualRunnable | back to summary |
|---|---|
| public Runnable contextualRunnable(Runnable runnable) Wraps a When
| |
| contextualSupplier | back to summary |
|---|---|
| public <R> Supplier Wraps a When
| |
| currentContextExecutor | back to summary |
|---|---|
| public Executor currentContextExecutor() Creates an Example usage:
The returned
| |
| withContextCapture | back to summary |
|---|---|
| public <T> CompletableFuture Returns a new The new completable future will use the same default executor as this ThreadContext,
which can be a ManagedExecutor if this ThreadContext was obtained by 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 When dependent stages are created from the new completable future, thread context is captured
and/or cleared as described in the documentation of the 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.
| |
| withContextCapture | back to summary |
|---|---|
| public <T> CompletionStage Returns a new The new completion stage will use the same default executor as this ThreadContext,
which can be a ManagedExecutor if this ThreadContext was obtained by 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 When dependent stages are created from the new completion stage, thread context is captured
and/or cleared as described in the documentation of the 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.
| |
Builder for ThreadContext instances.
Example usage:
ThreadContext threadContext = ThreadContext.builder()
.propagated(ThreadContext.APPLICATION, ThreadContext.SECURITY)
.unchanged(ThreadContext.TRANSACTION)
.build();
...
| Modifier and Type | Method and Description |
|---|---|
| public ThreadContext | Returns: new instance ofThreadContext.Builds a new |
| public ThreadContext. | |
| public ThreadContext. | Returns: the same builder instance upon which this method is invoked.types of thread context to capture and propagate. types)Defines the set of thread context types to capture from the thread that contextualizes an action or task. |
| public ThreadContext. | Returns: the same builder instance upon which this method is invoked.types of thread context to leave unchanged on the thread. types)Defines a set of thread context types that are essentially ignored, in that they are neither captured nor are they propagated or cleared from thread(s) that execute the action or task. |
| build | back to summary |
|---|---|
| public ThreadContext build() Builds a new After All created instances of
| |
| cleared | back to summary |
|---|---|
| public ThreadContext. Defines the set of thread context types to clear from the thread where the action or task executes. The previous context is resumed on the thread after the action or task ends. This set replaces the For example, if the user specifies
Constants for specifying some of the core context types are provided
on The MicroProfile Config property,
| |
| propagated | back to summary |
|---|---|
| public ThreadContext. Defines the set of thread context types to capture from the thread that contextualizes an action or task. This context is later re-established on the thread(s) where the action or task executes. This set replaces the Constants for specifying some of the core context types are provided
on The MicroProfile Config property, Thread context types which are not otherwise included in this set or
in the
| |
| unchanged | back to summary |
|---|---|
| public ThreadContext. Defines a set of thread context types that are essentially ignored, in that they are neither captured nor are they propagated or cleared from thread(s) that execute the action or task. This set replaces the Constants for specifying some of the core context types are provided
on The MicroProfile Config property, The configuration of For example, to run under the transaction of the thread of execution, with security context cleared and all other thread contexts propagated:
| |