Skip to content

Instantly share code, notes, and snippets.

@evnm evnm/FuturePool'd
Last active Aug 8, 2018

Embed
What would you like to do?
Comparing approaches to convert `java.util.concurrent.Future`s to `com.twitter.util.Future`s.
import com.twitter.util.{Future => TwitterFuture, FuturePool}
import java.util.concurrent.{Future => JavaFuture}
/**
* Convert a Java Future to a Twitter Future.
*/
def javaFutureToTwitterFuture[A](javaFuture: JavaFuture[A]): TwitterFuture[A] =
FuturePool.unboundedPool { javaFuture.get }
import com.twitter.util.{Future => TwitterFuture, _}
import java.util.concurrent.{Future => JavaFuture, TimeUnit}
/**
* Convert a Java Future to a Twitter Future.
*/
def javaFutureToTwitterFuture[A](
javaFuture: JavaFuture[A],
pollingInterval: Duration = Duration(50L, TimeUnit.MILLISECONDS)
): TwitterFuture[A] = {
val timer = new JavaTimer(isDaemon = false)
val promise = new Promise[A] {
timer.schedule(pollingInterval) {
if (javaFuture.isDone || javaFuture.isCancelled) {
timer.stop()
Try(javaFuture.get) match {
case Return(result) => setValue(result)
case Throw(t) => setException(t)
}
}
}
}
promise.setInterruptHandler { case _ => javaFuture.cancel(true) }
promise
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.