How to use Akka BoundedMailBox to throttle a producer

被刻印的时光 ゝ 提交于 2019-12-10 17:49:04

问题


I have two actors, one is producing messages and the other is consuming the messages at a fixed rate.

Is it possible to have the producer throttled by the consumers BoundedMailBox? (back pressure)

My producer is currently periodically scheduled (sending it a tick message), is there a way to have it scheduled by availability in the consumers mailbox instead?

I am using fire and forget style ( consumer.tell() ) since I do not need a response. Should I be using different message sending approach?


回答1:


Just specify a mailbox limit and it appears to block if the mailbox is full. I haven't tried this myself but the guys in this thread were looking at the behaviour and both found that the actor just blocks once the mailbox is at the limit.

See here for discussion and more testing of this nature. https://groups.google.com/forum/?fromgroups=#!topic/akka-user/e0tebq5V4nM

From that thread:

object ProducerConsumer extends App {

  implicit val system = ActorSystem("ProducerConsumer")

  def waitFor(actor: ActorRef) {
    Await.ready(gracefulStop(actor, 5.seconds), 5.seconds)
  }

  val consumers = system.actorOf(Props[Consumer].
    withRouter(RoundRobinRouter(4)).
    withDispatcher("consumer-dispatcher"), "consumer")

  for (work <- generateWork)
    consumers ! work

  consumers ! PoisonPill
  waitFor(consumers)
  system.shutdown
}

application.conf:

consumer-dispatcher {
  type = BalancingDispatcher
  mailbox-capacity = 100
}


来源:https://stackoverflow.com/questions/16500352/how-to-use-akka-boundedmailbox-to-throttle-a-producer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!