Skip to content

Instantly share code, notes, and snippets.

@thanhbv
Created September 21, 2013 10:40
Show Gist options
  • Save thanhbv/6649353 to your computer and use it in GitHub Desktop.
Save thanhbv/6649353 to your computer and use it in GitHub Desktop.
implicit convert spymemcache (>= 2.10.0) OperationFuture to scala.concurrent.Future
package sd.cb
import net.spy.memcached.internal.{OperationCompletionListener, OperationFuture}
import scala.concurrent.{Future, Promise}
/**
* @see http://stackoverflow.com/questions/11529145/how-do-i-wrap-a-java-util-concurrent-future-in-an-akka-future?rq=1
* http://stackoverflow.com/questions/17215421/scala-concurrent-future-wrapper-for-java-util-concurrent-future
*/
object CbFutureAsScala{
implicit def cbFutureAsScala[T](underlying: OperationFuture[T]): Future[T] = {
val p = Promise[T]()
underlying.addListener(new OperationCompletionListener{
def onComplete(f: OperationFuture[_]) {
val status = f.getStatus //f is underlying
//@see http://www.couchbase.com/issues/browse/JCBC-343#comment-68524
if(status.isSuccess)
p.success(underlying.get())
else
p.failure(new Exception(status.getMessage))
}
})
p.future
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment