I have an actor who sends a message to three actors, and waits for the response of all three actors in order to proceed. The actors return a datatype of the form: List[(String, Double, String)]. I want them all sorted according to the Double value of the Tuple3.
So far the code i've written is:
implicit val timeout = Timeout(5.seconds)
val x = actorOne ? "hey"
val y = actorTwo ? "you"
val z = actorThree ? "there"
val answer = for{
a <- x.mapTo[List[(String, Double, String)]]
b <- y.mapTo[List[(String, Double, String)]]
c <- z.mapTo[List[(String, Double, String)]]
} yield a ++ a ++ a sortBy(_._2)
How do i make sure the actor doesn't proceed until all three actors have responded?
Thanks
Your for-comprehension will only get evaluated after a, b, and c are evaluated, so you do not have to do anything there. If you mean that you have some later come which relies on the value of answer, then you can put it inside onComplete:
answer.onComplete {
case Success(x) => // do something on success
case Failure(ex) => // report failure
}
You can use Promise to interact with the results in the right time. E.g. if your outer method is supposed to return Future[Boolean], you can do like this:
def myFunction():Future[Boolean] = {
val p = Promise[Boolean]
implicit val timeout = Timeout(5.seconds)
val x = actorOne ? "hey"
val y = actorTwo ? "you"
val z = actorThree ? "there"
val answer = for{
a <- x.mapTo[List[(String, Double, String)]]
b <- y.mapTo[List[(String, Double, String)]]
c <- z.mapTo[List[(String, Double, String)]]
} yield a ++ a ++ a sortBy(_._2)
answer.onComplete {
case Success(x) =>
// do something with x
p.success(true)
case Failure(ex) =>
// process faliure
p.success(false)
}
p.future
}