Simple Akka mailbox configuration to discard overflowing messages

放肆的年华 提交于 2019-12-24 17:12:13

问题


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

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