How to return value on timeout from Akka 1.3 future? -


given following:

import akka.dispatch.{futuretimeoutexception, future}  val f1 = future({ thread.sleep(2000); 0}, 500)  f1.onexception {   case timeout: futuretimeoutexception => -1 }  f1.recover {     case timeout: futuretimeoutexception => -2 }  println(f1.get) 

why still exception? there way recover timeout such real value returned instead?

building off victor said, if want recover particular type of failure future using recover need aware recover returns new future , that's 1 need call get on in order recover functionality. this:

val f1 = future({ thread.sleep(2000); 0}, 500) val withrecover = f1.recover {   case timeout: futuretimeoutexception => -2 } println(withrecover.get) 

or chain onto future creation so:

val f1 = future({ thread.sleep(2000); 0}, 500).recover {   case timeout: futuretimeoutexception => -2 } println(f1.get) 

edit

so looks things work different in akka 1.3 own internal futures compared futures , promises scala 2.10. in akka 1.3, recover works non-timeout situations. rolled example of how work around that, should upgrade if possible on latest scala , akka:

import akka.dispatch._ import java.util.concurrent.timeunit.{ nanoseconds ⇒ nanos, milliseconds ⇒ millis } import akka.actor.actor  object futuretest {     def main(args: array[string]) {         val f1: future[int] = future({             thread.sleep(2000)             0         }, 500)          val f2 = recoverto(f1) {                         -2         }          println(f2.get)     }    def recoverto[t, >: t](fut:future[t])(f: => a): future[a] = {     val fa = new defaultcompletablefuture[a](fut.timeoutinnanos, nanos)     fut.ontimeout { future =>       fa completewithresult f     }     fut.oncomplete {       fa complete _.value.get     }         fa   }     } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -