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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With