Simple Akka Mailbox Configuration for Removing Overflowing Messages

I have a system with two members that have the same configuration. This desired configuration is: "the mailbox must have a maximum capacity of 1 message and any overflow message must be discarded."

What is the easiest way to fix this?

I tried to include the following in (Play Framework's) application.conf :

 akka.actor.default-mailbox { mailbox-type = "akka.dispatch.BoundedMailbox" mailbox-capacity = 1 } 

But this does not work: the actor mailbox still collects messages when busy and processes each of them when it is available. Either the actor does not give a damn about the contents of application.conf , or the above configuration is incorrect.

Any ideas?

+1
source share
2 answers

If you like a mailbox with a maximum capacity of 1 and discarding overflow 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 } } } 

Task execution after 0 ms, repeating every second:

 new Timer().schedule(MyTask, 0, 1000) 
-2
source

It is not recommended to set the default mailbox for all participants in the acting system to a limited mailbox with only one element, it is much better to configure your participants to use the specified mailbox, as described in the Akka documentation . Either through deployment, or directly in the code.

Your mailbox configuration also requires a push-timeout, which you can set to 0 if you want to cancel messages immediately.

+3
source

Source: https://habr.com/ru/post/957796/


All Articles