本文整理汇总了Scala中com.google.common.util.concurrent.FutureCallback类的典型用法代码示例。如果您正苦于以下问题:Scala FutureCallback类的具体用法?Scala FutureCallback怎么用?Scala FutureCallback使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FutureCallback类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: GuavaFutures
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.cassandra
import com.google.common.util.concurrent.{FutureCallback, Futures, ListenableFuture}
import scala.concurrent.{Future, Promise}
private[cassandra] object GuavaFutures {
implicit final class GuavaFutureOpts[A](val guavaFut: ListenableFuture[A]) extends AnyVal {
def asScala(): Future[A] = {
val p = Promise[A]()
val callback = new FutureCallback[A] {
override def onSuccess(a: A): Unit = p.success(a)
override def onFailure(err: Throwable): Unit = p.failure(err)
}
Futures.addCallback(guavaFut, callback)
p.future
}
}
}
示例2: toAsyncF
//设置package包名称以及导入依赖的类
package spinoco.fs2
import com.google.common.util.concurrent.{FutureCallback, Futures, ListenableFuture}
import fs2.util.Async
package object cassandra {
implicit def toAsyncF[F[_],A](f:ListenableFuture[A])(implicit F:Async[F]):F[A] = {
F.async { cb =>
F.pure(Futures.addCallback(f, new FutureCallback[A] {
def onFailure(t: Throwable): Unit = { cb(Left(t))}
def onSuccess(result: A): Unit = { cb(Right(result)) }
}))
}
}
}
示例3: executeWithAsynch
//设置package包名称以及导入依赖的类
package net.cassandratest
import com.datastax.driver.core.{ResultSetFuture, ResultSet, Session, Statement}
import com.google.common.util.concurrent.{ListeningExecutorService, FutureCallback, Futures}
import scala.concurrent.{ Promise, Future }
trait FutureHelper {
def executeWithAsynch(st: Statement)(implicit session: Session, executor: ListeningExecutorService): Future[ResultSet] = {
execute(st).future
}
private def execute(st: Statement)(implicit session: Session, executor: ListeningExecutorService): Promise[ResultSet] = {
val promise = Promise[ResultSet]()
val future: ResultSetFuture = session.executeAsync(st)
val callback = new FutureCallback[ResultSet] {
def onSuccess(result: ResultSet): Unit = {
promise success result
}
def onFailure(err: Throwable): Unit = {
promise failure err
}
}
Futures.addCallback(future, callback, executor)
promise
}
}
示例4: RichListenableFuture
//设置package包名称以及导入依赖的类
package recipestore.misc
import com.google.common.util.concurrent.{FutureCallback, Futures, ListenableFuture}
import scala.concurrent.{Future, Promise}
object RichListenableFuture {
implicit class RichListenableFuture[T](lf: ListenableFuture[T]) {
def asScala: Future[T] = {
val p = Promise[T]()
Futures.addCallback(lf, new FutureCallback[T] {
def onFailure(t: Throwable): Unit = p failure t
def onSuccess(result: T): Unit = p success result
})
p.future
}
}
}