Akka Actor Props factory -
Akka Actor Props factory -
akka , getting know each other.
from : akka 2.3.6 (current) actor recommended practice
these illustration actor called demoactor :
class demoactor(magicnumber: int) extends actor { def receive = { case x: int => sender() ! (x + magicnumber) } }
in recommended practices section of doc states : "it thought provide mill methods on companion object of each actor help keeping creation of suitable props close actor definition possible." :
object demoactor { def props(magicnumber: int): props = props(new demoactor(magicnumber)) }
question : what difference between specifying mill props method :
object demoactor { def props(magicnumber: int): props = props(classof[demoactor], magicnumber) }
in case missed it, difference argument props constructor :
new demoactor(magicnumber)
vs
classof[demoactor], magicnumber
from same akka documentation page bit farther in props section, mentions when using props(classof[actorwithargs], "arg1"
): "the presence of matching constructor verified during construction of props object, resulting in illegalargumenteception if no or multiple matching constructors found."
that's good, isn't it?!?....
that's good, isn't it?!?....
yes, improve if error can caught during compile time. advantage of invoking constructor straight compiler grab problem of no matching constructor instead of exception beingness thrown @ runtime.
an interesting thing props
apply
method when write:
props(new demoactor(magicnumber))
the constructor of actor not invoked when props instance created. constructor invocation passed by name rather by value. can see in signature of props
apply
method:
def apply[t <: actor](creator: ⇒ t)(implicit arg0: classtag[t]): props
notice right arrow in creator parameter. allows creator construction postponed, , potentially executed in process remote actors.
a potential hazard here if new
operation closes on scope , captures value not intended serialized or not serializable, making props object not serializable. why documentation recommends doing in companion object of actor—to minimize risk of closing on info not intended serialized.
akka actor
Comments
Post a Comment