Introduction to Akka Actors and Child Actors

Reading Time: 3 minutes
Introduction to Akka Actors and Child Actors

Introduction

In this blog, I will be explaining the basics of Akka, actors, and the way they are created. Therefore, I am naming this blog “Introduction to Akka Actors and Child Actors”. So let’s start with Akka first.

What is Akka?

Akka is not a framework but a toolkit and we use Akka for building distributed, highly concurrent, and fault-tolerant applications on the JVM, therefore Akka is based on a reactive manifesto and is used for building message-driven applications. In short, Akka is the implementation of the Actor model.

What is an Actor?

The Actor is the fundamental unit of computation. The Actor incorporates Processing, Storage, and Communication. “Processing” in Actor represents the behavior whereas “Storage” represents the state and the ”Communication” is the message flow between two or more Actors. Each Actor processes only one message.

We use ActorRef to represent each Actor. Similarly, we use ActorRef to send messages to the Actor.

We can send a message to an Actor and the Actor will handle that message.

When an Actor handles a message, it can –

  1. Create new Actors.
  2. Send a message to the Actors and,
  3. Change behavior for handling new messages.

Libraries for Creating Akka Actor

name := "Akka-actor-Demo"
version := "0.1"
scalaVersion := "2.13.5"

libraryDependencies ++=Seq( "com.typesafe.akka" %% "akka-actor" % "2.6.13",
                        "com.typesafe.akka" %% "akka-testkit" % "2.6.14" % Test
)
“com.typesafe.akka” %% “akka-actor” % “2.6.13” is used for creating Actors .
“com.typesafe.akka” %% “akka-testkit” % “2.6.14” % Test” is used for testing Actors .

Creating your first Akka Actor

import akka.actor.Actor

class FirstActor extends Actor
{
  def receive:Receive =
    {
      case msg : String => println("First Actor Created")
      case _ => println(s"My path is ${self.path.name}")
    }

}

In the above code –

In order to create an Actor, we have to extend the Actor trait and override the Receive method, where Receive method defines the behavior that is applied to the state.

Run Your First Actor

import akka.actor.{ActorSystem, Props}

object Main
{
  def main(args: Array[String]): Unit = {

    //to create First Actor
    val system = ActorSystem("First-Actor")
    val firstActor =system.actorOf(Props[FirstActor],"first-actor")
    firstActor ! "First Actor Created "
    firstActor ! 1
}
}

To successfully create and run our first Actor, we need to define an ActorSystem with a common name, where ActorSystem is the Root Actor to create top-level Actors.

Now call the “actorOf” method on the instance of ActorSystem to create an Actor where all the actor configurations are contained in the “Props” class.

Here we are sending two different messages, the first one is a String and the other one is a number of Int type.

Output –

First Actor Created and the msg is String
Any message other than string

How to Create a Child Actor?

In order to create a Child Actor, first of all, we need a Root Actor. We create Root Actor using the “actorOf” method of ActorSystem.

val rootActor = system.actorOf(Props[RootActor],"Root-Actor")
    rootActor ! "Create a child actor of Root Actor  "

Now inside the Receive method of Root-Actor class, we will create the Child Actor using Actor-Context ( used for creating Child Actor ) and will Prop the child class inside actorOf method that we have created on Actor Context instance.

After this, the Child Actor will send a message to the Child Actor class and Receive method of Child class will handle that message.

import akka.actor.Actor

class Child extends Actor
{

  def receive: Receive =
  {
    case msg : String => println(s"{ $msg }------ This is  Message from Child Actor ")
    case _ => println("Child actor created with other than String msg")
  }
}

Consequently, after running the Actors we will get the following output after running the Actors.

First Actor Created and the msg is String
Any message other than string 
Create a child actor of Root Actor   path is ======== Root-Actor 
{ hey im created in the world of concurrency  }------ This is  Message from Child Actor 

After doing all stuff with Actor, finally just terminate the ActorSystem using –

system.terminate()

So, in this blog, we learned the basics of Akka and creating Actors using Akka. Hope this worked to help you start with the process of learning in-depth about the Akka Toolkit.

Reference

Akka Documentation

Blog on Akka Actor

Written by 

Utkarsh Upadhyay is a Software Consultant at Knoldus Software LLP, having experience of more than 2 years. He has done B. Tech from IMS Engineering College, Ghaziabad. He has a decent knowledge of Scala, Lagom, Kafka, Cassandra, Akka, and Akka Stream. He loves playing and watching cricket and exploring new places.