How to create a priority based mailbox for an akka actor

There are some situations when you want an actor to process certain messages first and then other ones. So, I am going to demonstrate how to create a priority based mailbox for an actor.

Suppose we have an actor.

Now we want to process our messages in order like (string, Int , Long , other messages).

So what we need to do is to just write a priority based mailbox as below.

Here zero denotes highest priority.

After that add this configuration to application.conf.

At the time of actor creation specify the dispatcher class as below.

Suppose you send below messages to actor, in such a case string messages would be picked up first.

You can find the sample code here in github.

How to run the code.

After cloning the repo, go to the console and do sbt run in the project directory, it will show following output in console.

priority actor output

4 thoughts on “How to create a priority based mailbox for an akka actor

  1. Note that using this type of mailbox does not preserve message ordering, which you get with the default mailbox queue. So mixing this with for example data streams using Akka IO leads to pretty funny results.

Leave a Reply

%d bloggers like this: