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

时间:2016-07-05 05:09:24

标签: java playframework concurrency completable-future

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时,我们如何确保上下文可用?

1 个答案:

答案 0 :(得分:2)

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

来自the docs

  

ExecutionContexts保存当前线程的上下文ClassLoader和Http.Context。

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