首先,如果尚未完成操作,则应查看ThreadPoll配置文档。它应该为您提供Play默认使用的线程池以及如何对其进行调整的概述(您可能不需要特定的线程池来执行查询…)。
它包含许多技巧,包括配置针对JDBC操作优化的线程池的技巧。
现在,关于您的问题,您只需要使用supplyAsync
自定义ExecutorService
即可完成所需的工作。一个例子:
public CompletionStage<Result> getData() {
CompletableFuture<List<sqlRow>> cf = new CompletableFuture<>();
return cf.supplyAsync(() -> {
return Ebean.createsqlQuery("SELECT * FROM Users").findList();
}, ec) // <-- 'ec' is the ExecutorService you want to use
.thenApply(rows -> {
return ok(Json.toJson(rows));
});
}
请注意,从您的代码中,您正在使用Akka来获取ExecutionContext
(Scala),并且supplyAsync
期望使用Executor/ExecutorService
(Java)。因此,您将必须自己创建ServiceExecutor
并共享
// Thread pool with 10 threads
ExecutorService ec = Executors.newFixedThreadPool(10);
否则您将不得不在它们之间进行转换。要点应该可以帮助您实现