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 and understanding ActorSystem

In this small recipe, we will create and understand an ActorSystem. Since we are done with our initial setup of the Hello-Akka project, we don't need to make another project for it. We need to import the SBT project in an IDE like IntelliJ Idea.

Getting ready

If you are using IntelliJ Idea, then you must have Scala and the SBT plugin install on it.


For importing an SBT Scala project in IntelliJ Idea, visit the following website:

How to do it...

It is very easy to create an ActorSystem in Akka:

  1. Create a package com.packt.chapter1 inside the Hello-Akka src folder. We will keep all the source code in this package.
  2. Inside the package, create a file, say HelloAkkaActorSystem.scala, which will contain the code.
  3. Create a small scala application object, HelloAkkaActorSystem, and create an ActorSystem inside it:
        package com.packt.chapter1 
          * Created by user 
        object HelloAkkaActorSystem extends App { 
          val actorSystem = ActorSystem("HelloAkka") 
  1. Now, run the application in IntelliJ Idea or in the console. It will print the output as follows:

Downloading the Akka actor dependency


Here is a way to run a single Scala application using sbt from the console. Descend to the application root directory, and run the following command:sbt "runMain com.packt.chapter1.HelloAkkaActorSystem"

How it works...

In the recipe, we create a simple Scala object creating an ActorSystem, thereafter, we run the application.

Why we need ActorSystem

In Akka, an ActorSystem is the starting point of any Akka application that we write.

Technically, an ActorSystem is a heavyweight structure per application, which allocates n number of threads. Thus, it is recommended to create one ActorSystem per application, until we have a reason to create another one.

ActorSystem is the home for the actors in which they live, it manages the life cycle of an actor and supervises them.On creation, an ActorSystem starts three actors:

  • /user - The guardian actor: All user-defined actors are created as a child of the parent actor user, that is, when you create your actor in the ActorSystem, it becomes the child of the user guardian actor, and this guardian actor supervises your actors. If the guardian actor terminates, all your actors get terminated as well.
  • /system - The system guardian: In Akka, logging is also implemented using actors. This special guardian shut downs the logged-in actors when all normal actors have terminated. It watches the user guardian actor, and upon termination of the guardian actor, it initiates its own shutdown.
  • / - The root guardian: The root guardian is the grandparent of all the so-called top-level actors, and supervises all the top-level actors. Its purpose is to terminate the child upon any type of exception. It sets the ActorSystem status as terminated if the guardian actor is able to terminate all the child actors successfully.


For more information on ActorSystem visit: