MicroProfile Concurrency Spec PDF MicroProfile Concurrency Spec HTML MicroProfile Concurrency Spec Javadocs
Key features:
-
CompletableFuture
/CompletionStage
implementations with predictable thread context and using managed threads for async actions -
Ability to contextualize only specific actions/tasks
-
Compatibility with EE Concurrency
-
CDI injection as well as builder pattern
-
Configurable via MicroProfile Config
To get started, add this dependency to your project:
<dependency>
<groupId>org.eclipse.microprofile.concurrency</groupId>
<artifactId>microprofile-concurrency-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
Use the fluent builder API to construct a ManagedExecutor
:
ManagedExecutor executor = ManagedExecutor.builder()
.propagated(ThreadContext.APPLICATION, ThreadContext.CDI)
.maxAsync(5)
.build();
Then obtain a CompletableFuture
or CompletionStage
from the ManagedExecutor
, and from there use it the same as Java SE:
CompletableFuture<Integer> cf1 = executor.supplyAsync(supplier1)
.thenApplyAsync(function1)
.thenApply(function2);
Take care to shut down managed executors once the application no longer needs them:
executor.shutdownNow();
Similarly, you can construct ThreadContext
instances and use them to more granularly control thread propagation to individual stages:
ThreadContext secContext = ManagedExecutor.builder()
.propagated(ThreadContext.SECURITY)
.cleared(ThreadContext.TRANSACTION)
.unchanged(ThreadContext.ALL_REMAINING)
.build();
...
CompletableFuture<Void> stage2 = stage1.thenAccept(secContext.contextualConsumer(consumer1));