在我的web应用程序中,我正在使用Play!构建在Akka上的用于管理线程的框架。在一个特定的案例中,我编写了许多从外部服务收集数据的CompletionStages,我希望控制并行请求的数量,以便不会使这些外部服务负担过重。要做到这一点而不改变整个应用程序,一种方法是控制Akka使用的线程池大小。现在,我在akka like中准备了两个线程池,并尝试在这两个池之间切换。我正在使用类似下面的内容来组成我的CompletionStages:
CompletableFuture.completedFuture(firstResult)
.thenComposeAsync( firstResult -> { dostuff(firstResult);}根据Akka的documentation,设置当前线程池的方法如下:
// this is scala.concurrent.ExecutionContext
// for use with Futures, Scheduler, etc.
final ExecutionContext ex = system.dispatchers().lookup("my-dispatcher");通过观察我的应用程序,像这样设置上下文不会影响应用程序,并且只考虑默认的dispatcher。有没有办法在Akka中动态设置当前池的大小?
发布于 2018-03-16 20:40:37
您需要将自定义Executor传递给thenComposeAsync方法:
final java.util.concurrent.Executor exec = system.dispatchers().lookup("my-dispatcher");
CompletableFuture.completedFuture(firstResult)
.thenComposeAsync(firstResult -> {
dostuff(firstResult);
}, exec);https://stackoverflow.com/questions/49319161
复制相似问题