How to use custom executor in Akka dispatcher

前端 未结 1 1004
迷失自我
迷失自我 2021-01-19 00:17

I am trying to use a custom executor for a dispatcher in Akka.

Specifically, I want to wrap an existing executor with my own (logging, debugging, etc.).

I\'v

1条回答
  •  旧巷少年郎
    2021-01-19 00:55

    FQCN == Fully Qualified Class Name i.e. package name(s) + class name, ex:

    java.lang.String <- FQCN for String

    Here's an example of an ExecutorServiceConfigurator for the built in ThreadPoolExecutor:

    class ThreadPoolExecutorConfigurator(config: Config, prerequisites: DispatcherPrerequisites) extends ExecutorServiceConfigurator(config, prerequisites) {
    
      val threadPoolConfig: ThreadPoolConfig = createThreadPoolConfigBuilder(config, prerequisites).config
    
      protected def createThreadPoolConfigBuilder(config: Config, prerequisites: DispatcherPrerequisites): ThreadPoolConfigBuilder = {
        import akka.util.Helpers.ConfigOps
        ThreadPoolConfigBuilder(ThreadPoolConfig())
          .setKeepAliveTime(config.getMillisDuration("keep-alive-time"))
          .setAllowCoreThreadTimeout(config getBoolean "allow-core-timeout")
          .setCorePoolSizeFromFactor(config getInt "core-pool-size-min", config getDouble "core-pool-size-factor", config getInt "core-pool-size-max")
          .setMaxPoolSizeFromFactor(config getInt "max-pool-size-min", config getDouble "max-pool-size-factor", config getInt "max-pool-size-max")
          .configure(
            Some(config getInt "task-queue-size") flatMap {
              case size if size > 0 ⇒
                Some(config getString "task-queue-type") map {
                  case "array"       ⇒ ThreadPoolConfig.arrayBlockingQueue(size, false) //TODO config fairness?
                  case "" | "linked" ⇒ ThreadPoolConfig.linkedBlockingQueue(size)
                  case x             ⇒ throw new IllegalArgumentException("[%s] is not a valid task-queue-type [array|linked]!" format x)
                } map { qf ⇒ (q: ThreadPoolConfigBuilder) ⇒ q.setQueueFactory(qf) }
              case _ ⇒ None
            })
      }
    
      def createExecutorServiceFactory(id: String, threadFactory: ThreadFactory): ExecutorServiceFactory =
        threadPoolConfig.createExecutorServiceFactory(id, threadFactory)
    }
    

    Source: https://github.com/akka/akka/blob/v2.3.4/akka-actor/src/main/scala/akka/dispatch/AbstractDispatcher.scala#L344

    0 讨论(0)
提交回复
热议问题