繁体   English   中英

Akka - 用于阻塞操作的固定线程池 - 阻塞调度程序

[英]Akka - Fixed thread pool for Blocking Operations - Blocking Dispatcher

我已经为我的应用程序将要执行的阻塞操作配置了一个调度程序,如下所示:

engine {
  blocking-io-dispatcher {
     type = Dispatcher
     executor = "thread-pool-executor"
     thread-pool-executor {
       fixed-pool-size = 3
     }
       throughput = 1
  }
}

但是,我可以在日志中看到每 5 秒有 3 个以上的线程正在处理请求,(线程 6、16、5、7、15)

控制台输出:

system-engine.blocking-io-dispatcher-6
system-engine.blocking-io-dispatcher-16
system-engine.blocking-io-dispatcher-5
system-engine.blocking-io-dispatcher-7
system-engine.blocking-io-dispatcher-15

请在下面找到代码:

import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
import java.util.Date
import akka.routing.RoundRobinRouter
import akka.routing.DefaultResizer

class SendPushNotificationActor extends Actor {
   def receive = {
      case SendPushNotificationMessage(message) => {
      println(Thread.currentThread().getName)
      // Simulate blocking operation which takes 5 seconds to send the push notification message
      Thread.sleep(5000)
    }
  }
}

object SendPushNotificationTest {

  def main(args: Array[String]): Unit = {
    val system = ActorSystem("system")

    val sendPushNotificationActor = system.actorOf(Props[SendPushNotificationActor].withDispatcher("engine.blocking-io-dispatcher")
  .withRouter(RoundRobinRouter(nrOfInstances = 5)))

    for (i <- 1.to(100)) {
      sendPushNotificationActor ! SendPushNotificationMessage("Push Notification Message")
    }
  }
}

case class SendPushNotificationMessage(message: String)

任何有关如何修复池的帮助将不胜感激。

池中的线程可能由于某种原因死亡,在这种情况下,可以立即产生新线程以替换死线程并使池保持固定大小。 新线程被赋予递增的 ID,这是您在日志文件中看到的。 线程池的大小肯定是固定的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM