简体   繁体   中英

PlayFramework passing execution context when using CompletableFutures and java

In this section of the documentation they explain how to do this for scala

Future {
  // Some blocking or expensive code here
}(myExecutionContext)

What is the equivalent of this when you use CompletableFuture :

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

How do we make sure context is available when using java and CompletableFuture ?

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

From the docs :

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

See also the docs for play.libs.concurrent.HttpExecutionContext .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM