代码之家  ›  专栏  ›  技术社区  ›  user51

为什么actorSystem没有创建要与自定义dispatcher一起运行的actor

  •  0
  • user51  · 技术社区  · 3 年前

    嗨,文件中有下面的typesafe配置 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(test)

    我想让这个演员在自定义线程池上运行,但它没有发生。我怎样才能达到同样的效果?

    0 回复  |  直到 3 年前
        1
  •  0
  •   Levi Ramsey    3 年前

    akka.actor.typed.DispatcherSelector (延伸到 akka.actor.typed.Props )对应于所需的调度器。

    产卵时 ActorSystem 在自定义调度程序上,只能传递 Props 通过超载 Config 或者一个 ActorSystemSetup .

    如果要重写用户guardian actor的actor(具有您传递到 动作系统

     akka.actor.default-dispatcher {
       executor = "thread-pool-executor"
    
       thread-pool-executor {
         fixed-pool-size = 40
       }
       throughput = 2
     }
    
    推荐文章