PlayFramework 在使用 CompletableFutures 和 java 时传递执行上下文

PlayFramework passing execution context when using CompletableFutures and java

this section of the documentation 中,他们解释了如何为 scala

执行此操作
Future {
  // Some blocking or expensive code here
}(myExecutionContext)

当你使用 CompletableFuture 时,这相当于什么:

CompletableFuture.supplyAsync(() -> {
  // Some blocking or expensive code here
}, someCustomExecutor);

我们如何确保在使用 javaCompletableFuture 时上下文可用?

CompletableFuture.supplyAsync(() -> {
    // Some blocking or expensive code here
}, play.libs.concurrent.HttpExecution.defaultContext());

来自the docs

ExecutionContexts that preserve the current thread's context ClassLoader and Http.Context.

另请参阅 play.libs.concurrent.HttpExecutionContext 的文档。