Creating Actors

You typically create objects and invoke methods on them. An actor is also an object, but you never call methods on it directly. Instead you send messages and each actor is backed by a message queue. If an actor is busy processing a message, then the messages that arrive are queued—the senders aren’t blocked; they fire-and-forget. At most, one message is processed on an actor at any given time. Actors are born and built with thread safety.

Let’s define an actor.

ProgrammingActors/HollywoodActor.scala
 
import​ akka.actor._
 
 
class​ HollywoodActor() ​extends​ Actor {
 
def​ receive = {
 
case​ message => println(s​"playing the role of $message"​)
 
}
 
}

Scala uses actors from Akka—a very powerful reactive library written in ...

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.