Book Image

Akka Cookbook

By : Vivek Mishra, Héctor Veiga Ortiz
Book Image

Akka Cookbook

By: Vivek Mishra, Héctor Veiga Ortiz

Overview of this book

Akka is an open source toolkit that simplifies the construction of distributed and concurrent applications on the JVM. This book will teach you how to develop reactive applications in Scala using the Akka framework. This book will show you how to build concurrent, scalable, and reactive applications in Akka. You will see how to create high performance applications, extend applications, build microservices with Lagom, and more. We will explore Akka's actor model and show you how to incorporate concurrency into your applications. The book puts a special emphasis on performance improvement and how to make an application available for users. We also make a special mention of message routing and construction. By the end of this book, you will be able to create a high-performing Scala application using the Akka framework.
Table of Contents (18 chapters)
Title Page
About the Authors
About the Reviewer
Customer Feedback

Prioritizing messages that an actor receives

There are situations when you want your actor to process some particular messages first, and then move on to others. This means you want to give priority to some messages over others.

For such scenarios, Akka has comes up with priority mailbox, which lets you prioritize messages.


Getting ready

To step through this recipe, we need to import our Hello-Akka project in IDE-like IntelliJ Idea. Prerequisites are the same as those in previous recipes.

How to do it...

  1. Create a Scala file named PriorityMailBox.scala in package comi.packt.chapter1.
  2. Create an actor called MyPriorityActor as follows:
        class MyPriorityActor extends Actor { 
          def receive: PartialFunction[Any, Unit] = { 
            // Int Messages 
            case x: Int => println(x) 
            // String Messages 
            case x: String => println(x) 
            // Long messages 
            case x: Long => println(x) 
            // other messages 
            case x => println(x) 
  1. To prioritize the messages, create a priority mailbox as follows:
        class MyPriorityActorMailbox(settings:
          ActorSystem.Settings, config: Config) extends 
          UnboundedPriorityMailbox ( 
         // Create a new PriorityGenerator,
           lower prio means more important 
           PriorityGenerator { 
             // Int Messages 
             case x: Int => 1 
             // String Messages 
             case x: String => 0 
             // Long messages 
             case x: Long => 2 
             // other messages 
             case _ => 3 


  1. Add this configuration to application.conf:
        prio-dispatcher {  
          mailbox-type = 
  1. Create an application, PriorityMailBoxApp, as shown in the following code:
        object PriorityMailBoxApp extends App { 
           val actorSystem = ActorSystem("HelloAkka") 
          val myPriorityActor = 
          myPriorityActor ! 6.0 
          myPriorityActor ! 1 
          myPriorityActor ! 5.0 
          myPriorityActor ! 3 
          myPriorityActor ! "Hello" 
          myPriorityActor ! 5 
          myPriorityActor ! "I am priority actor" 
          myPriorityActor ! "I process string messages first,then 
           integer, long and others" 
  1. Run the application in IDE or from the console. The following output will be displayed:
      I process string messages first,then integer, long and others
      I am priority actor

How it works...

In step two, we just define an actor which processes Int, Long, String, and other messages.

In step four, we configure a prio-dispatcher with this MyPriorityActorMailbox.

In step five, we create an actor which will use the prio-dispatcher.

In step six, as we can see in the output, the string messages are processed first, because they were given highest priority.