user8215502
user8215502

Reputation: 201

akka - how to get results of several actors?

I'm wondering if there any best practice to send 2 messages to 2 different actors

and wait for all of them (get results of course )in order to continue executing.

i.e something like:

send message to actor 1
send message to actor 2
List<results> = wait.all(actor1,actor2)

Upvotes: 0

Views: 304

Answers (1)

thwiegan
thwiegan

Reputation: 2173

You are probably looking for the ask pattern in combination with a Future.sequence or a for-comprehension:

import akka.pattern.ask

case object Request

implicit val timeout = Timeout(5 seconds) // needed for `?` below

// Ask your actors for a result
val f1 = actorA ? Request
val f2 = actorB ? Request
val f3 = actorC ? Request

// for-comprehension
(for {
    x <- f1
    s <- f2
    d <- f3
} yield (f1, f2, f3)).map {
    case (r1, r2, r3) =>
        //Do your stuff with the results
}

// Future.sequence
Future.sequence(f1, f2, f3).map {
    case (r1, r2, r3) =>
        //Do your stuff with the results
}

Upvotes: 1

Related Questions