akka Hello world Simple Actor Implementation


Consider a communication happening between a Employee and its HR Department.

Actor System Message Flow

Broadly these are explained in the following six steps when a message is passed to the actor:

  1. Employee creates something called an ActorSystem.

  2. It uses the ActorSystem to create something called as ActorRef. The message(MSG) is sent to the ActorRef (a proxy to HR Actor).

  3. Actor ref passes the message along to a Message Dispatcher.

  4. The Dispatcher enqueues the message in the target Actor’s MailBox.

  5. The Dispatcher then puts the Mailbox on a Thread (more on that in the next section).

  6. The MailBox dequeues a message and eventually delegates that to the actual HR Actor’s receive method.

    /** The Main Program consider it as a Employee Actor that is sending the requests  **/

    object EmployeeActorApp extends App{
     //Initialize the ActorSystem
      val actorSystem=ActorSystem("HrMessageingSystem")

     //construct the HR Actor Ref
      val hrActorRef=actorSystem.actorOf(Props[HrActor])

     //send a message to the HR Actor

     //Let's wait for a couple of seconds before we shut down the    system
      Thread.sleep (2000) 

     //Shut down the ActorSystem.


    /** The HRActor reads the message sent to it and performs action based on the message Type **/
    class HRActor extends Actor {
       def receive  = {
            case s: String if(s.equalsIgnoreCase(“SICK”)) => println("Sick Leave applied”)
            case s: String if(s.equalsIgnoreCase(“PTO”)) => println("PTO applied “)