Akka:如何使平衡池中的参与者数量相对于线程池大小

Akka: how to make number of actors in balancing pool relative to thread pool size

对于在我的应用程序中包含关键计算的 Actor class,我在路由器后面生成了一堆 actor:

val concurrency = 4 // to be replaced by something dynamic
val ahoCorasick = AppActorSystem.system.actorOf(MyActorClass.props(ldb)
                                         .withRouter(BalancingPool(nrOfInstances = concurrency)), 
                                          name = "foo") 

如何获取相对于核心数或适用于 Actor 系统的线程池大小的 Actor 实例数?例如每个核心一个 Actor,还是等于提供的线程池大小的多个 actor? (也可能定义特定于这些参与者的线程池)。

最好从您的 Akka 配置中获取线程数(reference.confapplication.conf)。这是解释其配置方式的参考 doc。您可以使用自定义调度程序或默认调度程序,所以最好不要盲目使用下面的代码,而是了解您实际使用的是哪个调度程序。

你可以这样做:

import akka.actor.ActorSystem
import com.typesafe.config.{Config, ConfigFactory}

val conf = ConfigFactory.load().getConfig("akka.actor.default-dispatcher.fork-join-executor")

def getThreadConf(conf: Config): (Int, Int, Int) = {
  val parallelismFactor = conf.getInt("parallelism-factor")
  val parallelismMin = conf.getInt("parallelism-min")
  val parallelismMax = conf.getInt("parallelism-max")
  (parallelismFactor, parallelismMin, parallelismMax)
}

println(getThreadConf(conf))
val system = ActorSystem()
println(getThreadConf(system.settings.config.getConfig("akka.actor.default-dispatcher.fork-join-executor")))

默认你得到:

factor: 3, min: 8, max: 64

检查您正在使用的调度程序名称或使用 val myActor = context.actorOf(Props[MyActor].withDispatcher("my-dispatcher"), "myactor1")

明确设置一个