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
- Root Actor
- Denoted by path
/
- This is the parent of all actors in the system, and the last one to stop when the system itself is terminated
- Denoted by path
- System Actor
- Denoted by
/system
- All actors created automatically by the actor system come under the umbrella of this actor
- Denoted by
- User Actor
- Denoted by
/user
- 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.
- Denoted by
1.2 Components of an Actor System
An actor system consists of different system.
- Actors
We already looked at different types of actors in the previous section.- Root Guardian
- System Guardian
- User Guardian
- Dead Letter Office
- It is used to store all undelivered messages.
- Any time a message is destined for an actor that either doesn’t exist or is not running, goes to the dead letter office.
- Configuration system
It is used for configuring Akka and the application.
- Dispatcher
- Dispatchers are responsible for selecting an actor and it’s messages and assigning them the CPU
- Default Dispatcher of Akka
-
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.
-
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.
-
- Scheduler
- Light-weight scheduler for running asynchronous tasks after some deadline
in the future. Not terribly precise but cheap.
- Light-weight scheduler for running asynchronous tasks after some deadline
- Event Streams
- Main event bus of this actor system, used for example for logging
- 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.
- Mailboxes
Mailbox
holds the messages that are destined for anActor
2.0 How message is delivered to an actor
- Messages are sent to an actor through an
ActorRef
Dispatcher
is responsible for placing the messages onto the actor’sMailbox
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 theRunnable
interface, it is simply scheduled onto theExecutorService
implementation backing up theDispatcher
. - 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.
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.
-
-
In the next blog of the Learn Akka tutorial series, we will look at Supervision Model of Akka Actors.