[2.0] Handling Akka Future Timeouts

1,000 views
Skip to first unread message

Malax

unread,
Mar 15, 2012, 7:14:07 AM3/15/12
to play-fr...@googlegroups.com
Hi!

I currently use something like

AsyncResult {
    (extractor ? Extract(uri)).asPromise.map { result =>
        Ok(result.toString)
    }
}

to do some work initiated from an Action. As with all ask requests to an Akka Actor it can time out. How can I handle those timeouts gracefully in play?

I couldn't find a way to catch the timeout somehow. I tried a pattern match in the map call, looking for a "null" value (Because I get akka.pattern.AskTimeoutException: null in the logs if I run into a timeout) and wrapping the map in a try catch - neither did work.

Promises seem to have a timeout mechanism on their own, but .asPromise does not magically map the Akka timeout to the promise timeout mechanism, does it?

Hope someone can bring me back on track!
Malax


Derek Williams

unread,
Mar 15, 2012, 10:47:09 AM3/15/12
to play-fr...@googlegroups.com
On Thu, Mar 15, 2012 at 5:14 AM, Malax <the....@gmail.com> wrote:
to do some work initiated from an Action. As with all ask requests to an Akka Actor it can time out. How can I handle those timeouts gracefully in play?


Akka 2.0 timeouts are handled by completing the Future with an exception (I believe it's an AskTimeoutException, which extends Java's TimeoutException). Exceptions in Akka Futures can be handled with the 'recover' method:

AsyncResult {
  (actor ? msg map { result =>
    Ok(result.toString)
  } recover {
    case e: TimeoutException => Ok("Timed out")
  }).asPromise
}

recover works very much like 'catch' in a 'try/catch' block.

I'm not sure of the best way of handling exceptions like this when using Play's Promise, so I usually delay converting it as much as possible (hoping that Play will convert over to Scala's Promise/Future in 2.10)
 
--
Derek Williams

Malax

unread,
Mar 15, 2012, 11:55:31 AM3/15/12
to play-fr...@googlegroups.com
Thanks, Derek! Will fiddle with this a bit. :-)

Am Donnerstag, 15. März 2012 15:47:09 UTC+1 schrieb Derek Williams:
Reply all
Reply to author
Forward
0 new messages