Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>@Viktor Klang: We understand that <code>j.u.c.Future</code> is an abomination. But that's what we're getting back from a piece of software we must accept as given for the time being.</p> <p>So far, this is what we've hacked together:</p> <pre><code>def wrapJavaFutureInAkkaFuture[T](javaFuture: java.util.concurrent.Future[T], maybeTimeout: Option[Duration] = None)(implicit system: ActorSystem): akka.dispatch.Future[T] = { val promise = new akka.dispatch.DefaultPromise[T] pollJavaFutureUntilDoneOrCancelled(javaFuture, promise, maybeTimeout.map(_.fromNow)) promise } </code></pre> <p>In other words, create a separate Akka <code>Promise</code> (the write-side of a <code>Future</code>) corresponding to the <code>j.u.c.Future</code>, kicks off the callback <code>pollJavaFutureUntilDoneOrCancelled</code> to update the Promise by polling the "abomination", and returns the Promise to the caller.</p> <p>So how do we "poll" to update the Akka Promise based on the state of the j.u.c.Future?</p> <pre><code>def pollJavaFutureUntilDoneOrCancelled[T](javaFuture: java.util.concurrent.Future[T], promise: akka.dispatch.Promise[T], maybeDeadline: Option[Deadline] = None)(implicit system: ActorSystem) { if (maybeDeadline.exists(_.isOverdue)) javaFuture.cancel(true); if (javaFuture.isDone || javaFuture.isCancelled) { promise.complete(allCatch either { javaFuture.get }) } else { Play.maybeApplication.foreach { implicit app =&gt; system.scheduler.scheduleOnce(50 milliseconds) { pollJavaFutureUntilDoneOrCancelled(javaFuture, promise, maybeDeadline) } } } } </code></pre> <p>This is an attempt at what was hinted at in the google groups discussion that I referenced in the question. It uses the Akka scheduler to call itself back every 50 ms to check if the j.u.c.Future is either done or cancelled. Whenever that happens, it updates the Akka Promise with the completed state.</p> <p>@Victor Klang, et al: </p> <p>Is this best practice? Do you know of a better way to do this? Are we missing a downside here that we should know about?</p> <p>Thanks for any more help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload