本文整理汇总了Scala中com.google.common.util.concurrent.ListenableFuture类的典型用法代码示例。如果您正苦于以下问题:Scala ListenableFuture类的具体用法?Scala ListenableFuture怎么用?Scala ListenableFuture使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ListenableFuture类的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: point
//设置package包名称以及导入依赖的类
package deaktator.advtut.futures.instances
import java.util.concurrent.Callable
import com.google.common.util.concurrent.{AsyncFunction, Futures, ListeningExecutorService, ListenableFuture}
import deaktator.advtut.futures.cats.Monad
import com.google.common.base.{Function => GFunction}
import deaktator.advtut.futures.instances.ListenableFutureInstances._
override def point[A](asyncExecutedValue: => A): ListenableFuture[A] = {
exSvc.submit(new Callable[A]{
override def call(): A = asyncExecutedValue
})
}
override def flatMap[A, B](ma: ListenableFuture[A])(f: A => ListenableFuture[B]): ListenableFuture[B] =
Futures.transform(ma, GAsyncFn(f))
override def map[A, B](fa: ListenableFuture[A])(f: A => B): ListenableFuture[B] =
Futures.transform(fa, GFn(f)) // flatMap(fa)(a => point(f(a)))
override def ap[A, B](x: ListenableFuture[A])(f: ListenableFuture[A => B]): ListenableFuture[B] =
flatMap(f)(g => map(x)(y => g(y))) // map2(x, f)((y, g) => g(y))
override def map2[A, B, C](x: ListenableFuture[A], y: ListenableFuture[B])(f: (A, B) => C): ListenableFuture[C] =
flatMap(y)(b => map(x)(a => f(a, b))) // ap(y)(map(x)(f.curried))
}
object ListenableFutureInstances {
object Implicits {
implicit def listenableFutureInstances(implicit exSvc: ListeningExecutorService): ListenableFutureInstances =
ListenableFutureInstances(exSvc)
}
case class GFn[A, B](f: A => B) extends GFunction[A, B] {
def apply(a: A): B = f(a)
}
case class GAsyncFn[A, B](f: A => ListenableFuture[B]) extends AsyncFunction[A, B] {
def apply(a: A): ListenableFuture[B] = f(a)
}
}
示例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
}
}
}