Почему ActorSystem не создает актера для работы с настраиваемым диспетчером
Привет, у вас есть конфигурация безопасного типа в файле
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
}
Ниже приведен код актера, типизированный для акка.
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 Получил PrintMessageAny(test) в потоке PrintAnyTypeMessage-akka.actor.default-dispatcher-6
Я хочу, чтобы этот актер работал в пуле пользовательских потоков, но этого не происходит. Как я могу добиться того же?
1 ответ
You associate the dispatcher with the actor when you spawn it, by passing an
akka.actor.typed.DispatcherSelector
(which extends
akka.actor.typed.Props
) corresponding to the desired dispatcher.
When spawning the on a custom dispatcher, one can only pass
Props
through the overloads that take either a
Config
or an
ActorSystemSetup
.
If wanting to override the actor for the user guardian actor (the actor with the behavior you passed into the
ActorSystem
), может иметь смысл сделать этот диспетчер диспетчером по умолчанию:
akka.actor.default-dispatcher {
executor = "thread-pool-executor"
thread-pool-executor {
fixed-pool-size = 40
}
throughput = 2
}