Top Description Inners Fields Methods
org.eclipse.microprofile.context

public Interface ThreadContext

Imports
java.util.concurrent.CompletableFuture, .CompletionStage, .Callable, .Executor, .ExecutorService, java.util.function.Consumer, .BiConsumer, .BiFunction, .Function, .Supplier, org.eclipse.microprofile.context.spi.ContextManagerProvider, .ContextManager

This interface offers various methods for capturing the context of the current thread and applying it to various interfaces that are commonly used with completion stages and executor services. This allows you to contextualize specific actions that need access to the context of the creator/submitter of the stage/task.

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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ThreadContext.Builder

Builder for ThreadContext instances.

Field Summary

Modifier and TypeField and Description
public static final String
ALL_REMAINING

Identifier for all available thread context types which are not specified individually under cleared, propagated, or unchanged.

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.

Method Summary

Modifier and TypeMethod and Description
public static ThreadContext.Builder

Returns:

a new Builder instance.
builder
()

Creates a new Builder instance.

public <
callable result type.
R
>
Callable<R>

Returns:

contextualized proxy instance that wraps execution of the call method with context.
contextualCallable
(Callable<R>
instance to contextualize.
callable
)

Wraps a Callable with context that is captured from the thread that invokes contextualCallable.

public <
type of first parameter to consumer.
T
,
type of second parameter to consumer.
U
>
BiConsumer<T, U>

Returns:

contextualized proxy instance that wraps execution of the accept method with context.
contextualConsumer
(BiConsumer<T, U>
instance to contextualize.
consumer
)

Wraps a BiConsumer with context that is captured from the thread that invokes contextualConsumer.

public <
type of parameter to consumer.
T
>
Consumer<T>

Returns:

contextualized proxy instance that wraps execution of the accept method with context.
contextualConsumer
(Consumer<T>
instance to contextualize.
consumer
)

Wraps a Consumer with context that is captured from the thread that invokes contextualConsumer.

public <
type of first parameter to function.
T
,
type of second parameter to function.
U
,
function result type.
R
>
BiFunction<T, U, R>

Returns:

contextualized proxy instance that wraps execution of the apply method with context.
contextualFunction
(BiFunction<T, U, R>
instance to contextualize.
function
)

Wraps a BiFunction with context that is captured from the thread that invokes contextualFunction.

public <
type of parameter to function.
T
,
function result type.
R
>
Function<T, R>

Returns:

contextualized proxy instance that wraps execution of the apply method with context.
contextualFunction
(Function<T, R>
instance to contextualize.
function
)

Wraps a Function with context that is captured from the thread that invokes contextualFunction.

public Runnable

Returns:

contextualized proxy instance that wraps execution of the run method with context.
contextualRunnable
(Runnable
instance to contextualize.
runnable
)

Wraps a Runnable with context that is captured from the thread that invokes ContextualRunnable.

public <
supplier result type.
R
>
Supplier<R>

Returns:

contextualized proxy instance that wraps execution of the supply method with context.
contextualSupplier
(Supplier<R>
instance to contextualize.
supplier
)

Wraps a Supplier with context captured from the thread that invokes contextualSupplier.

public Executor

Returns:

an executor that wraps the execute method with context.
currentContextExecutor
()

Creates an Executorthat runs tasks on the same thread from which executeis invoked but with context that is captured from the thread that invokes currentContextExecutor.

public <
completable future result type.
T
>
CompletableFuture<T>

Returns:

the new completable future.
withContextCapture
(CompletableFuture<T>
a completable future whose completion triggers completion of the new completable future that is created by this method.
stage
)

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

public <
completion stage result type.
T
>
CompletionStage<T>

Returns:

the new completion stage.
withContextCapture
(CompletionStage<T>
a completion stage whose completion triggers completion of the new stage that is created by this method.
stage
)

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

Field Detail

ALL_REMAININGback to summary
public static final String ALL_REMAINING

Identifier for all available thread context types which are not specified individually under cleared, propagated, or unchanged.

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.

See Also
ManagedExecutor.Builder#cleared, ManagedExecutor.Builder#propagated, ThreadContext.Builder
APPLICATIONback 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.

See Also
ManagedExecutor.Builder#cleared, ManagedExecutor.Builder#propagated, ThreadContext.Builder
CDIback 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 @RequestScoped resource:


   @RequestScoped
   public class MyRequestScopedBean {
       public String getState() {
         // returns some request-specific information to the caller
       }
   }
   
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.

   ManagedExecutor exec = ManagedExecutor.builder()
       .propagated(ThreadContext.CDI, ThreadContext.APPLICATION)
       .build();

   @Inject
   MyRequestScopedBean requestBean;

   @GET
   public void foo() {
       exec.supplyAsync(() -> {
           String state = reqBean.getState();
           // do some work with the request state
       }).thenApply(more -> {
           // request state also available in future stages
       });
   }
   
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.
See Also
ManagedExecutor.Builder#cleared, ManagedExecutor.Builder#propagated, ThreadContext.Builder
NONEback 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 builder.propagated(ThreadContext.NONE) rather than builder.propagated(new String[0]) or builder.propagated(), all of which have the same meaning.

When using MicroProfile Config to specify defaults, the value None indicates an empty array. For example,

mp.context.ThreadContext.unchanged=None
or
mp.context.ManagedExecutor.propagated=None
SECURITYback 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.

See Also
ManagedExecutor.Builder#cleared, ManagedExecutor.Builder#propagated, ThreadContext.Builder
TRANSACTIONback 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.

See Also
ManagedExecutor.Builder#cleared, ManagedExecutor.Builder#propagated, ThreadContext.Builder

Method Detail

builderback to summary
public static ThreadContext.Builder builder()

Creates a new Builder instance.

Returns:ThreadContext.Builder

a new Builder instance.

contextualCallableback to summary
public <R> Callable<R> contextualCallable(Callable<R> callable)

Wraps a Callable with context that is captured from the thread that invokes contextualCallable.

When call is invoked on the proxy instance, context is first established on the thread that will run the call method, then the call method of the provided Callable is invoked. Finally, the previous context is restored on the thread, and the result of the Callable is returned to the invoker.

Parameters
<R>
callable result type.
callable:Callable<R>

instance to contextualize.

Returns:Callable<R>

contextualized proxy instance that wraps execution of the call method with context.

Exceptions
IllegalArgumentException:
if an already-contextualized Callable is supplied to this method.
contextualConsumerback to summary
public <T, U> BiConsumer<T, U> contextualConsumer(BiConsumer<T, U> consumer)

Wraps a BiConsumer with context that is captured from the thread that invokes contextualConsumer.

When accept is invoked on the proxy instance, context is first established on the thread that will run the accept method, then the accept method of the provided BiConsumer is invoked. Finally, the previous context is restored on the thread, and control is returned to the invoker.

Parameters
<T>
type of first parameter to consumer.
<U>
type of second parameter to consumer.
consumer:BiConsumer<T, U>

instance to contextualize.

Returns:BiConsumer<T, U>

contextualized proxy instance that wraps execution of the accept method with context.

Exceptions
IllegalArgumentException:
if an already-contextualized BiConsumer is supplied to this method.
contextualConsumerback to summary
public <T> Consumer<T> contextualConsumer(Consumer<T> consumer)

Wraps a Consumer with context that is captured from the thread that invokes contextualConsumer.

When accept is invoked on the proxy instance, context is first established on the thread that will run the accept method, then the accept method of the provided Consumer is invoked. Finally, the previous context is restored on the thread, and control is returned to the invoker.

Parameters
<T>
type of parameter to consumer.
consumer:Consumer<T>

instance to contextualize.

Returns:Consumer<T>

contextualized proxy instance that wraps execution of the accept method with context.

Exceptions
IllegalArgumentException:
if an already-contextualized Consumer is supplied to this method.
contextualFunctionback to summary
public <T, U, R> BiFunction<T, U, R> contextualFunction(BiFunction<T, U, R> function)

Wraps a BiFunction with context that is captured from the thread that invokes contextualFunction.

When apply is invoked on the proxy instance, context is first established on the thread that will run the apply method, then the apply method of the provided BiFunction is invoked. Finally, the previous context is restored on the thread, and the result of the BiFunction is returned to the invoker.

Parameters
<T>
type of first parameter to function.
<U>
type of second parameter to function.
<R>
function result type.
function:BiFunction<T, U, R>

instance to contextualize.

Returns:BiFunction<T, U, R>

contextualized proxy instance that wraps execution of the apply method with context.

Exceptions
IllegalArgumentException:
if an already-contextualized BiFunction is supplied to this method.
contextualFunctionback to summary
public <T, R> Function<T, R> contextualFunction(Function<T, R> function)

Wraps a Function with context that is captured from the thread that invokes contextualFunction.

When apply is invoked on the proxy instance, context is first established on the thread that will run the apply method, then the apply method of the provided Function is invoked. Finally, the previous context is restored on the thread, and the result of the Function is returned to the invoker.

Parameters
<T>
type of parameter to function.
<R>
function result type.
function:Function<T, R>

instance to contextualize.

Returns:Function<T, R>

contextualized proxy instance that wraps execution of the apply method with context.

Exceptions
IllegalArgumentException:
if an already-contextualized Function is supplied to this method.
contextualRunnableback to summary
public Runnable contextualRunnable(Runnable runnable)

Wraps a Runnable with context that is captured from the thread that invokes ContextualRunnable.

When run is invoked on the proxy instance, context is first established on the thread that will run the run method, then the run method of the provided Runnable is invoked. Finally, the previous context is restored on the thread, and control is returned to the invoker.

Parameters
runnable:Runnable

instance to contextualize.

Returns:Runnable

contextualized proxy instance that wraps execution of the run method with context.

Exceptions
IllegalArgumentException:
if an already-contextualized Runnable is supplied to this method.
contextualSupplierback to summary
public <R> Supplier<R> contextualSupplier(Supplier<R> supplier)

Wraps a Supplier with context captured from the thread that invokes contextualSupplier.

When supply is invoked on the proxy instance, context is first established on the thread that will run the supply method, then the supply method of the provided Supplier is invoked. Finally, the previous context is restored on the thread, and the result of the Supplier is returned to the invoker.

Parameters
<R>
supplier result type.
supplier:Supplier<R>

instance to contextualize.

Returns:Supplier<R>

contextualized proxy instance that wraps execution of the supply method with context.

Exceptions
IllegalArgumentException:
if an already-contextualized Supplier is supplied to this method.
currentContextExecutorback to summary
public Executor currentContextExecutor()

Creates an Executorthat runs tasks on the same thread from which executeis invoked but with context that is captured from the thread that invokes currentContextExecutor.

Example usage:

Executor contextSnapshot = threadContext.currentContextExecutor();
...
// from another thread, or after thread context has changed,
contextSnapshot.execute(() -> obj.doSomethingThatNeedsContext());
contextSnapshot.execute(() -> doSomethingElseThatNeedsContext(x, y));

The returned Executor must raise IllegalArgumentException if an already-contextualized Runnable is supplied to its execute method.

Returns:Executor

an executor that wraps the execute method with context.

withContextCaptureback to summary
public <T> CompletableFuture<T> withContextCapture(CompletableFuture<T> stage)

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 ManagedExecutor#getThreadContext() or the default executor service if provided by the platform (which can be done via ContextManager.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 java.lang.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.

Parameters
<T>
completable future result type.
stage:CompletableFuture<T>

a completable future whose completion triggers completion of the new completable future that is created by this method.

Returns:CompletableFuture<T>

the new completable future.

withContextCaptureback to summary
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 ManagedExecutor#getThreadContext() or the default executor service if provided by the platform (which can be done via ContextManager.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 java.lang.UnsupportedOperationException for all *Async methods that do not specify an executor. For example, thenRunAsync(Runnable).

When dependent stages are created from the new completion stage, 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 stage or any dependent stages created from it, other than the new dependent completion stage that is created by this method.

Parameters
<T>
completion stage result type.
stage:CompletionStage<T>

a completion stage whose completion triggers completion of the new stage that is created by this method.

Returns:CompletionStage<T>

the new completion stage.

org.eclipse.microprofile.context back to summary

public Interface ThreadContext.Builder


Builder for ThreadContext instances.

Example usage:

 ThreadContext threadContext = ThreadContext.builder()
                                                  .propagated(ThreadContext.APPLICATION, ThreadContext.SECURITY)
                                                  .unchanged(ThreadContext.TRANSACTION)
                                                  .build();
...

Method Summary

Modifier and TypeMethod and Description
public ThreadContext

Returns:

new instance of ThreadContext.
build
()

Builds a new ThreadContext instance with the configuration that this builder represents as of the point in time when this method is invoked.

public ThreadContext.Builder

Returns:

the same builder instance upon which this method is invoked.
cleared
(String...
types of thread context to clear from threads that run actions and tasks.
types
)

Defines the set of thread context types to clear from the thread where the action or task executes.

public ThreadContext.Builder

Returns:

the same builder instance upon which this method is invoked.
propagated
(String...
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.Builder

Returns:

the same builder instance upon which this method is invoked.
unchanged
(String...
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.

Method Detail

buildback to summary
public ThreadContext build()

Builds a new ThreadContext instance with the configuration that this builder represents as of the point in time when this method is invoked.

After build is invoked, the builder instance retains its configuration and may be further updated to represent different configurations and build additional ThreadContext instances.

All created instances of ThreadContext are destroyed when the application is stopped. The container automatically shuts down these ThreadContext instances and raises IllegalStateException to reject subsequent attempts to apply previously captured thread context.

Returns:ThreadContext

new instance of ThreadContext.

Exceptions
IllegalStateException:
for any of the following error conditions
  • if one or more of the same context types appear in multiple of the following sets: (cleared, propagated, unchanged)
  • if a thread context type that is configured to be cleared or propagated is unavailable
  • if context configuration is neither specified on the builder nor via MicroProfile Config, and the builder implementation lacks vendor-specific defaults of its own.
  • if more than one ThreadContextProvider has the same thread context type
clearedback to summary
public ThreadContext.Builder cleared(String... types)

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 cleared set that was previously specified on the builder instance, if any.

For example, if the user specifies ThreadContext#TRANSACTION in this set, then a transaction is not active on the thread when the action or task runs, such that each action or task is able to independently start and end its own transactional work.

ThreadContext#ALL_REMAINING is automatically appended to the set of cleared context if neither the propagated set nor the unchanged set include ThreadContext#ALL_REMAINING.

Constants for specifying some of the core context types are provided on ThreadContext. Other thread context types must be defined by the specification that defines the context type or by a related MicroProfile specification.

The MicroProfile Config property, mp.context.ThreadContext.cleared, establishes a default that is used if no value is otherwise specified. The value of the MicroProfile Config property can be the empty string or a comma separated list of context type constant values.

Parameters
types:String[]

types of thread context to clear from threads that run actions and tasks.

Returns:ThreadContext.Builder

the same builder instance upon which this method is invoked.

propagatedback to summary
public ThreadContext.Builder propagated(String... types)

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 propagated set that was previously specified on the builder instance, if any.

Constants for specifying some of the core context types are provided on ThreadContext. Other thread context types must be defined by the specification that defines the context type or by a related MicroProfile specification.

The MicroProfile Config property, mp.context.ThreadContext.propagated, establishes a default that is used if no value is otherwise specified. The value of the MicroProfile Config property can be the empty string or a comma separated list of context type constant values.

Thread context types which are not otherwise included in this set or in the unchanged set are cleared from the thread of execution for the duration of the action or task.

Parameters
types:String[]

types of thread context to capture and propagate.

Returns:ThreadContext.Builder

the same builder instance upon which this method is invoked.

unchangedback to summary
public ThreadContext.Builder unchanged(String... 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.

This set replaces the unchanged set that was previously specified on the builder instance.

Constants for specifying some of the core context types are provided on ThreadContext. Other thread context types must be defined by the specification that defines the context type or by a related MicroProfile specification.

The MicroProfile Config property, mp.context.ThreadContext.unchanged, establishes a default that is used if no value is otherwise specified. The value of the MicroProfile Config property can be the empty string or a comma separated list of context type constant values. If a default value is not specified by MicroProfile Config, then the default value is an empty set.

The configuration of unchanged context is provided for advanced patterns where it is desirable to leave certain context types on the executing thread.

For example, to run under the transaction of the thread of execution, with security context cleared and all other thread contexts propagated:

 ThreadContext threadContext = ThreadContext.builder()
                                                  .unchanged(ThreadContext.TRANSACTION)
                                                  .cleared(ThreadContext.SECURITY)
                                                  .propagated(ThreadContext.ALL_REMAINING)
                                                  .build();
...
task = threadContext.contextualRunnable(new MyTransactionlTask());
...
// on another thread,
tx.begin();
...
task.run(); // runs under the transaction due to 'unchanged'
tx.commit();
Parameters
types:String[]

types of thread context to leave unchanged on the thread.

Returns:ThreadContext.Builder

the same builder instance upon which this method is invoked.