Steffen
Steffen

Reputation: 8073

How to change the timeout in composed futures?

In the example from the akka 1.1 documentation on composing futures, I'm wondering how it's possible to programmatically set the timeout of the generated future. I'm aware that I can adjust the global timeout in akka.conf, but I want to do it in place only for this piece of code.

The example code looks as follows

val f1 = actor1 !!! msg1
val f2 = actor2 !!! msg2

val f3 = for {
   a: Int    <- f1
   b: Int    <- f2
   c: String <- actor3 !!! (a + b)
} yield c

val result = f3.get()

Am I right that akka creates a total of four futures in this example?

It's easy to change the timeouts in the first case, e.g.

val f1 = actor1 !!! (msg1, 15000)  // sets timeout to 15 seconds

but how can I Set the timeout for the wrapping future? Any ideas?

Upvotes: 6

Views: 478

Answers (1)

derekjw
derekjw

Reputation: 386

There would actually be 6 futures total I believe, the 3 you got from the actors, and 1 for each flatMap/map method.

The timeout for the 3 actor futures are set just like you stated, but the 3 wrapper futures will inherit the timeout from the future on the right hand side of each section of the for-comprehension. However, the returned future will use the timeout of the first future in the for-comprehension.

For Akka 2.0 this will change to allow an implicit timeout to be specified, as long as no issues pop up during testing.

Upvotes: 6

Related Questions