Re: [akka-user] Future.onTimeout

99 views
Skip to first unread message

√iktor Ҡlang

unread,
Aug 30, 2012, 3:17:57 PM8/30/12
to akka...@googlegroups.com
Hi Eric,

On Thu, Aug 30, 2012 at 8:58 PM, Eric Pederson <eri...@gmail.com> wrote:
Hi all:

The Actors doc page (http://doc.akka.io/docs/akka/2.0.3/scala/actors.html) refers to a Future.onTimeout but this doesn't actually exist.


Yup, this has been corrected for the upcoming 2.1
 
I am interested in a way to get a callback if a future times out, without resorting to blocking using Await.ready.  For example:  f.onSuccess(s).onFailure(f).onTimeout(t)

This is inside of an actor.   My workaround right now is to create the future with onSuccess and onFailure and then schedule a message to be sent to self at the timeout time.   If the timeout message comes and the Future hasn't returned then do something.  This works but it's a lot of bookkeeping.


You could essentially lift it in verbatim into your codebase.

Cheers,
 

Thanks,

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://akka.io/faq/
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user?hl=en.
 
 



--
Viktor Klang

Akka Tech Lead
Typesafe - The software stack for applications that scale

Twitter: @viktorklang

√iktor Ҡlang

unread,
Aug 30, 2012, 5:46:02 PM8/30/12
to akka...@googlegroups.com


On Thu, Aug 30, 2012 at 11:36 PM, Eric Pederson <eri...@gmail.com> wrote:
Thanks Viktor -

I copied it over got it working after tweaking to make it compatible with Akka 2.0/Scala 2.9.

I ran into the same issue as Timothy from August 11 where you recommended that he do this:

after(1.second, using = system.scheduler)(Future.failed(new java.util.concurrent.TimeoutException("ohnoes"))) either future onComplete { 

I needed to make a Akka 2.0 compatible version of either() too:

  def eitherFuture[T, U >: T](f1: Future[T], f2: Future[U])(implicit ec: ExecutionContext): Future[U] = {
    val p = Promise[U]()
    val completePromise = { e: Either[Throwable, U] => p tryComplete e }

    f1 onComplete completePromise
    f2 onComplete completePromise

    p.future
  }

Future.firstCompletedOf(Seq(
after(1.second, using = system.scheduler)(Future.failed(new java.util.concurrent.TimeoutException("ohnoes"))),
future
)) onComplete { … }
 

To be honest, it's not super-intuitive to have to provide your own failed Future and use either().  It would great to add your tip to Timothy to the Scaladoc. 

I'll open a ticket about clarifying it in the FAQ/docs!

Cheers,
 

Thanks!

√iktor Ҡlang

unread,
Aug 30, 2012, 6:50:06 PM8/30/12
to akka...@googlegroups.com


On Thu, Aug 30, 2012 at 11:49 PM, Eric Pederson <eri...@gmail.com> wrote:
Ahh - I *thought* I had seen something like either() in Akka 2.0.   I missed it this time - doh!   Thanks Viktor!

You're more than welcome!

Happy hAkking,

Cheers,
Reply all
Reply to author
Forward
0 new messages