Anatomy of an actor
Before diving into a full-blown application, let's look at the different components of the actor framework and how they fit together:
Mailbox: A mailbox is basically a queue. Each actor has its own mailbox. When you send a message to an actor, the message lands in its mailbox and does nothing until the actor takes it off the queue and passes it through its
receive
method.Messages: Messages make synchronization between actors possible. A message can have any type with the sole requirement that it should be immutable. In general, it is better to use case classes or case objects to gain the compiler's help in checking message types.
Actor reference: When we create an actor using
val echo1 = system.actorOf(Props[EchoActor])
,echo1
has typeActorRef
. AnActorRef
is a proxy for an actor and is what the rest of the world interacts with: when you send a message, you send it to theActorRef
, not to the actor directly. In fact, you can never obtain a handle to an actor directly...