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 CDI to inject a ManagedExecutor or ThreadContext service:
public class MyBean {
@Inject ManagedExecutor executor;
Or you can create one using a builder:
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);