AKKA Basic Introduction

AKKA Basic Introduction

You can see about intro of AKKA on the below medium blog, I will focus on coding stuff.
https://medium.com/techno101/introduction-to-akka-java-7d909456e6bd

Setup Akka

You should first import AKKA into your project to use it. To import add the below line of code in your build.sbt file.

libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.8.4"

You can find different dependencies on the Maven repository website.

Creating an actor system

Creating an actor system is very resource expensive so you should only create one actor system in your project. To create an actor system add the following line of code to your scala object inside a main method or extend App.

import akka.actor.{Actor, ActorSystem, Props}    // Necessary Imports
val actorSystem = ActorSystem("firstActorSystem")    // Creating an actor system
println(actorSystem.name)    // Printing the name of actor system

The `firstActorSystem` is the name of the actor system.

Creating an actor

To create an actor you need to create a class that extends Actor.

class WordCountActor extends Actor {
}

After extending an Actor you need to override the receive method which has the return type of PartialFunction[Any, Unit] (which is also written as Receive).

class WordCountActor extends Actor {
  override def receive: Receive = {
  }
}

You can implement the internal data & behaviour of the actor inside the receive method.

class WordCountActor extends Actor {
  // Internal Data
  var totalWords = 0
  // behaviour
  override def receive: Receive = {
    case message: String =>
      println(s"[word counter] I have received: $message")
      totalWords += (message.split(" ")).length
    case msg => println(s"[word counter] I cannot understand ${msg.toString}")
  }
}

Instantiate an actor

After creating an actor you need to instantiate it to use it.

val wordCounter = actorSystem.actorOf(Props(WordCountActor()), "wordCounter")

This actor is like a worker who is the means to communicate with the actor from the WordCountActor class. I'm calling it "wordCounter." Think of it like hiring a worker for a specific job. I can now give tasks to this worker by sending messages to it, and I keep a reference to this worker in a variable called wordCounter so I can talk to it whenever I need to.

Communicate with the actor

To communicate with the actor you need to call the tell method on the reference of the actor you want to communicate with.

wordCounter ! "I am learning AKKA and it's going to be fun."

Actor with parameters

Have you wondered how to pass the message to the actor who receives it's name from the parameter (parameterized class)?

class Person(name: String) extends Actor {
  override def receive: Receive = {
    case "hi" => println(s"Hi, my name is $name.")
    case _ =>
  }
}

We can instantiate the actor in 2 ways:

  • Bad and discouraged method

      val person = actorSystem.actorOf(Props(Person("Bob")))  // Discouraged method
    
  • Good and encouraged method

      object Person {    // Create a companion object of the actor class
        def props(name: String) = Props(Person(name))
      }
      val person = actorSystem.actorOf(Person.props("Bob"))