Why the actorSystem is not creating the actor to run with custom dispatcher

245 views Asked by At

Hi have below typesafe config in file 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
    }

Below is the akka-typed actor code.

    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)
    }

When I run the code, I get the below output.

PrintMeActor Application started in Thread PrintAnyTypeMessage-akka.actor.default-dispatcher-6 Got PrintMessageAny(test) in Thread PrintAnyTypeMessage-akka.actor.default-dispatcher-6

I want this actor to run on the custom-thread-pool but it is not happening. How can I achieve the same?

1

There are 1 answers

0
Levi Ramsey On BEST ANSWER

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 ActorSystem 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), it may make more sense to make that dispatcher the default dispatcher:

 akka.actor.default-dispatcher {
   executor = "thread-pool-executor"

   thread-pool-executor {
     fixed-pool-size = 40
   }
   throughput = 2
 }