In this blog we will look at different components of Actor System, how message is delviered to actors, and also the actor life cycle.

This blog is part of the tutorial series – Learn Akka, in which we will learn about using Akka with Java.

Table of Contents

1.0 Actor System

We saw earlier that Actor System is the core to the Akka actor model.

An Actor System is a hierarchical group of actors which share common configuration, e.g. dispatchers, deployments, remote capabilities and addresses.

Lets look into detail some of its components

1.1 Different types of actors in actor system

There are 3 different types of actors in an actor system

  1. Root Actor
    1. Denoted by path /
    2. This is the parent of all actors in the system, and the last one to stop when the system itself is terminated
  2. System Actor
    1. Denoted by /system
    2. All actors created automatically by the actor system come under the umbrella of this actor
  3. User Actor
    1. Denoted by /user
    2. This is the parent actor for all user created actors. Every actor we create using the Akka library will have the constant path /user/ prepended to it.

actor_top_tree

1.2 Components of an Actor System

An actor system consists of different system.

  1. Actors
    We already looked at different types of actors in the previous section.

    1. Root Guardian
    2. System Guardian
    3. User Guardian
  2. Dead Letter Office
    1. It is used to store all undelivered messages.
    2. Any time a message is destined for an actor that either doesn’t exist or is not running, goes to the dead letter office.
  3. Configuration system
    It is used for configuring Akka and the application.

  4. Dispatcher

    1. Dispatchers are responsible for selecting an actor and it’s messages and assigning them the CPU
    2. Default Dispatcher of Akka
      1. This dispatcher is backed by a thread pool, creates one mailbox per actor, and can easily be shared between a number of actors of different types.

      2. This dispatcher will pick an actor and assign it a dormant thread from it’s pool. The actor will then process a certain number of messages from its mailbox before relinquishing the thread.

  5. Scheduler
    1. Light-weight scheduler for running asynchronous tasks after some deadline
      in the future. Not terribly precise but cheap.
  6. Event Streams
    1. Main event bus of this actor system, used for example for logging
    2. An Akka EventStream is a pub-sub stream of events both system and user generated, where subscribers are ActorRefs and the channels are Classes and Events are any java.lang.Object.
  7. Mailboxes
    Mailbox holds the messages that are destined for an Actor

Top ∆

2.0 How message is delivered to an actor

actor-model-components-flow

  • Messages are sent to an actor through an ActorRef
  • Dispatcher is responsible for placing the messages onto the actor’s Mailbox which is essentially backed by queue data structure.
  • The Dispatcher is again responsible for running the mailbox at a certain point. As the mailbox itself is an implementation of the Runnableinterface, it is simply scheduled onto the ExecutorService implementation backing up the Dispatcher.
  • At that point one of the available threads executes the run() method of the mailbox which simply dequeues the message and passes it through the message processing logic of the current actor.

Top ∆

3.0 Actor Life cycle

  • When any actorOf() method is called, an Actor is created and goes into an Initialised state.

  • Before an actor is fully started and it can receive any message, the method preStart() is called.

  • A running actor, when it gets a stop Signal goes to Stopped state

  • Before an actor fully terminates, the method postStop() is called.

  • On Exception it goes into Affected state, from where it can do 3 things
    • Stop the actor
    • Resume operation
    • Restart the actor. Following methods will be called when actor gets restarted
      • postStop() -> before existing actor Terminates
      • preRestart() -> Before it is restarted
      • postRestart() -> After actor is restarted
      • preStart() -> Before it is started and ready to receive messages.

actors-lifecycle

Top ∆


In the next blog of the Learn Akka tutorial series, we will look at Supervision Model of Akka Actors.