Hello there,
In general always perfer tell (!) instead of ask (?), unless you have a
compelling reason not to. It has less overhead, and I also find things nicer to
model using plain actor tell's instead of ask's - which mix up styles a little
bit (actors in general "fire and forget").
But let's also look at your example, because I think you may have mixed up what
Future[_] gets completed when. The futures don't automatically get composed,
but by chanining of reactions to a futures completion, actors can "cascade"
completing their corresponding futures.
In the bellow example we use ask twice. The a actor completes the fa future,
but only once the fb future completes successfuly. Completing an "ask created
Future", happens when an actor replies to the message sent by ask - so in the
end => b completes the fb future, which then triggers the map in a, which
completes the fa future.
import akka.pattern.ask
val b = context.system.actorOf(Props(new Actor {
def receive = {
case s => sender() ! s // this response completes the `fb` future
}
}))
val a = context.system.actorOf(Props(new Actor {
def receive = {
case s =>
val replyTo = sender()
val fb = b ? (s + "!")
fb map { bResponse =>
// this runs when the b actor replies - thus, completing the `fb`
future
replyTo ! bResponse // this completes the `fa` future
}
}
}))
val fa: Future[Any] = a ? "wat" // this future will be completed by the `a`
actor replying to the "wat" message
--
Konrad Malawski
geecon.org / java.pl / krakowscala.pl / gdgkrakow.pl
From: glidester [email protected]
Reply: [email protected] [email protected]
Date: 21 March 2014 at 11:49:05
To: [email protected] [email protected]
Subject: [akka-user] Chaining actor ask calls
Hi,
I'm in the process of learning Akka and I wondered if anyone could clarify
something for me.
If I have 2 actors. I 'ask' Actor A which in turn 'asks' Actor B.
This means I get returned a Future from ActorB wrapped in a Future from Actor A
(correct?)
Is this good practice? Should I be using "tell" instead to avoid the nested
Futures?
Any pointers gratefully received!
G
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ:
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.