Akka Actors Explained
What makes an Actor
In the previous post we wrote our “hello world” program for Akka. In this post we’ll take a closer look at what makes an actor.
previous post:
So the gist of this post is this:
an actor exists in an actor system and encapsulates state and behavior. The state of the actor changes when it responds to messages that it gets from its mailbox.
Actor System
An actor is the smallest unit of computation. An application designed to use the actor pattern would consist of a number of actors communicating with each other. There’d even be supervisor actors. All of these needs to exist somewhere. An actor system is the universe in which actors exist. The system is responsible for managing its actors, their behavior, configuration, etc.
Actor
Like I stated before, an actor is the smallest unit of computation. Concurrent programs are split into multiple actors and each actor performs a specific subtask. Actors never expose their state and this state only changes in response to messages that it processes. They can even change their behavior in response to messages.
Each actor is a reactive, event-driven, lightweight thread.
State
An actor can be stateful or stateless. If it is stateful, the state of the actor is defined by the values of its variables, any reference to external resources, etc. This state only changes in response to messages and is never exposed directly to other actors. Also, when an actor fails and restarts, its state values are restored so that it behaves consistently.
Behavior
The behavior is the computational logic that is executed in response to a message and may result in the state of the actor being updated. Additionally, the behavior itself may change in response to messages i.e. the behavior of an actor can be swapped in and out. Also, when an actor fails and restarts, it is restarted with its default behavior.
Mailbox
Whenever a message is sent to an actor, it is enqueued in the actor’s mailbox and is processed from there. Each actor has one and only one mailbox associated with it. Akka provides multiple mailbox implementations. The mailbox can be bounded or unbounded. It may even be priority-based. In a priority mailbox, messages are enqueued based on a priority and messages with higher priority are processed first.
The process of dequeuing a message and and handing it over to the actor for processing is done by the dispatcher. We’ll take a closer look at mailboxes and dispatchers later.
That’s it for this post.