Actor construction
Actor construction is a common source of difficulty for people new to Akka. Unlike (most) ordinary objects, you never instantiate actors explicitly. You would never write, for instance, val echo = new EchoActor
. In fact, if you try this, Akka raises an exception.
Creating actors in Akka is a two-step process: you first create a Props
object, which encapsulates the properties needed to construct an actor. The way to construct a Props
object differs depending on whether the actor takes constructor arguments. If the constructor takes no arguments, we simply pass the actor class as a type parameter to Props
:
val echoProps = Props[EchoActor]
If we have an actor whose constructor does take arguments, we must pass these as additional arguments when defining the Props
object. Let's consider the following actor, for instance:
class TestActor(a:String, b:Int) extends Actor { ... }
We pass the constructor arguments to the Props
object as follows:
val testProps = Props(classOf[TestActor...