问题
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