Akka 为什么actorSystem没有创建要与自定义调度程序一起运行的参与者
Hi在文件Akka 为什么actorSystem没有创建要与自定义调度程序一起运行的参与者,akka,akka-typed,akka-dispatcher,Akka,Akka Typed,Akka Dispatcher,Hi在文件application typed.conf中有以下类型安全配置 akka { loggers = ["akka.event.slf4j.Slf4jLogger"] loglevel = "DEBUG" logging-filter = "akka.event.slf4j.Slf4jLoggingFilter" actor { provider = &qu
application typed.conf
中有以下类型安全配置
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = "DEBUG"
logging-filter = "akka.event.slf4j.Slf4jLoggingFilter"
actor {
provider = "local"
}
}
custom-thread-pool {
type = Dispatcher
executor = "thread-pool-executor"
thread-pool-executor {
fixed-pool-size = 40
}
throughput = 2
}
下面是akka类型的演员代码
import akka.actor.typed.{ActorSystem, Behavior, DispatcherSelector, PostStop, Signal}
import akka.actor.typed.scaladsl.AbstractBehavior
import akka.actor.typed.scaladsl.ActorContext
import akka.actor.typed.scaladsl.Behaviors
import com.typesafe.config.ConfigFactory
import scala.concurrent.ExecutionContext
trait PrintMessage
case class PrintMessageAny(x: Any) extends PrintMessage
object PrintMeActor {
def apply(): Behavior[PrintMessage] =
Behaviors.setup[PrintMessage](context => new PrintMeActor(context))
}
class PrintMeActor(context: ActorContext[PrintMessage]) extends AbstractBehavior[PrintMessage](context) {
val dispatcherSelector: DispatcherSelector = DispatcherSelector.fromConfig("custom-thread-pool")
implicit val executionContext: ExecutionContext = context.system.dispatchers.lookup(dispatcherSelector)
println(s"PrintMeActor Application started in Thread ${Thread.currentThread().getName}")
override def onMessage(msg: PrintMessage): Behavior[PrintMessage] = {
// No need to handle any messages
println(s"Got $msg in Thread ${Thread.currentThread().getName}")
Behaviors.same
}
override def onSignal: PartialFunction[Signal, Behavior[PrintMessage]] = {
case PostStop =>
context.log.info("PrintMeActor Application stopped")
this
}
}
object TestTypedActorApp extends App {
val config = ConfigFactory.load("application-typed.conf")
val as: ActorSystem[PrintMessage] = ActorSystem(PrintMeActor(), "PrintAnyTypeMessage", config)
as.tell(PrintMessageAny("test"))
Thread.sleep(2000)
}
当我运行代码时,我得到以下输出
PrintMeActor应用程序在线程PrintAnyTypeMessage-akka.actor.default-dispatcher-6中启动
在线程PrintAnyTypeMessage-akka.actor.default-dispatcher-6中获取了PrintMessageAny(测试)
我想让这个演员在自定义线程池上运行,但它没有发生。如何实现相同的功能?在生成调度程序时,通过传递与所需调度程序对应的
akka.actor.typed.DispatcherSelector
(扩展了akka.actor.typed.Props
)将调度程序与actor关联起来
在自定义调度程序上生成ActorSystem
时,只能通过采用Config
或ActorSystemSetup
的重载传递Props
如果希望为用户guardian actor(具有您传递到ActorSystem
的行为的actor)覆盖actor,则将该dispatcher设置为默认dispatcher可能更有意义:
akka.actor.default-dispatcher {
executor = "thread-pool-executor"
thread-pool-executor {
fixed-pool-size = 40
}
throughput = 2
}