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

Creating a control-aware mailbox for an actor

There are some situations when you want your actor to process a certain message first before any other message, at any point of time. This means that you can tell an actor to do some particular work before doing any other job.

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 of the previous recipes.

How to do it...

  1. Create a file, ControlAwareMailbox.scala, in package com.packt.chapter1.
  2. Add the following imports to the top of the file:
        import akka.dispatch.ControlMessage 
        import{Props, Actor, ActorSystem} 
  1. Create a control message case object as follows:
        case object MyControlMessage extends ControlMessage 
  1. Define an actor:
        class Logger extends Actor {  
          def receive = { 
            case MyControlMessage => println("Oh, I have to process
              Control message first") 
            case x => println(x.toString) 


  1. Add the following configuration to application.conf:
         control-aware-dispatcher {  
          mailbox-type = 
          //Other dispatcher configuration goes here 
  1. Create a test application in which we can send a message to the preceding application, and it will process the control message first:
        object ControlAwareMailbox extends App {  
          val actorSystem = ActorSystem("HelloAkka") 
          val actor = 
          actor ! "hello" 
          actor ! "how are" 
          actor ! "you?" 
          actor ! MyControlMessage 
  1. Run the application in IDE or from the console. You will get the following output:
      Oh, I have to process Control message first
      how are

How it works...

In step three, we create an object, MyControlMessage, which extends the ControlMessage.

ControlMessage is a trait. The message which extends this trait will be handled on priority by ControlAwareMailbox. ControlAwareMailbox maintains two queues to allow messages that extend ControlMessage to be delivered with priority.

In step four, we create an actor which will handle ControlMessage.

In step five, we configure the control-aware-dispatcher in application.conf.


In step six. we create the actor with control-aware-dispatcher.

In step seven, we are able to see in the output that the actor processed ControlMessage first.