O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required