问题
I have a system with 2 actors, which share the same configuration. This desired configuration is: "the mailbox should have a max capacity of 1 message, and any overflowing message should be discarded".
What is the easiest way to set this up?
I have tried putting the following in (Play Framework's) application.conf
:
akka.actor.default-mailbox {
mailbox-type = "akka.dispatch.BoundedMailbox"
mailbox-capacity = 1
}
But it doesn't work: the actor's mailbox still piles up messages when busy, and processes each of them when available. Either the actor does not give a damn about the contents of application.conf
, or the above config is incorrect.
Any ideas?
回答1:
It's not a good idea to set the default mailbox for all actors in the actor system to a bounded mailbox with only one item, it's much better to configure your actors to use a specified mailbox as described in the Akka documentation . Either through deployment or directly in code.
Your mailbox configuration also needs a push-timeout that you can set to 0 if you want to discard messages immediately.
回答2:
If like me you want a mailbox with a max capacity of 1, and discarding overflowing messages, I recommend using java.util.Timer
instead of Akka.
This is what I wrote in my Scala program:
MyTask.scala:
object MyTask extends TimerTask {
var isRunning = false;
def run() {
if (!isRunning) {
isRunning = true
[...]
isRunning = false
}
}
}
Executing the task after 0ms, repeating every second:
new Timer().schedule(MyTask, 0, 1000)
来源:https://stackoverflow.com/questions/19816148/simple-akka-mailbox-configuration-to-discard-overflowing-messages