scala - Akka actor ask pattern does not work as expected -
actor b contains reference actor called senderr
. actor ask
s actor b, waits response , prints it. not receive response. why? must print number 4 @ console doesn't.
class a(b: actorref) extends actor { private implicit val timeout = timeout(20 seconds) b ! 1 def receive = { case 2 => (b ? 3).map(println) } } class b extends actor { var senderr : actorref = null def receive = { case 1 => senderr = sender() sender ! 2 case 3 => senderr ! 4 } } object main extends app { val system = actorsystem("test") val b = system.actorof(props[b]) val = system.actorof(props(classof[a], b)) }
ask
creates temporary micro-actor single purpose of receiving 1 response of type any
when b
receives 3
sends 4
senderr
refers a
. a
not have match 4
. in order (b ? 3).map(println)
receive 4
, b
has send sender()
, @ time refers temporary actor set ask
:
case 3 => sender ! 4
alternatively, have case 4
in a
wouldn't need use ask
Comments
Post a Comment