MicroProfile® Context Propagation 1.0

Context Propagation 1.0

Description

MicroProfile Context Propagation 1.0 provides APIs for obtaining CompletableFutures that are backed by managed threads (threads that are managed by the container), with the ability to capture context from the thread that creates the CompletableFuture and apply it when running the CompletionStage action.  This enables CompletionStage actions to rely on having predictable thread context and enables users of these CompletionStages to guard against unintentional donation of thread context.

Example of ManagedExecutor, which contextualizes all dependent stages:

ManagedExecutor executor = ManagedExecutor.builder()
.propagated(ThreadContext.CDI, ThreadContext.APPLICATION)
.build();
...
CompletableFuture<Integer> stage = executor
.supplyAsync(supplier1)
.thenApplyAsync(function1)
.thenApply(function2);

Example of ThreadContext for granular contextualization of a single stage:

ThreadContext threadContext = ThreadContext.builder()
.propagated(ThreadContext.APPLICATION)
.cleared(ThreadContext.SECURITY, ThreadContext.TRANSACTION)
.unchanged(ThreadContext.ALL_REMAINING)
.build();
...
Function<Integer, Double> getItemCost = threadContext.contextualFunction(id -> {
// java:comp, java:module, java:app lookups require the application's context
DataSource ds = InitialContext.doLookup("java:app/env/jdbc/ds1");
...
});
unmanagedStage4 = unmanagedStage1.thenApply(getProductId)
.thenApply(getItemCost)
.thenApply(addToTotal);

Conforms To UI/UX Guidelines
Not verified