当前位置: 首页>>代码示例>>Scala>>正文


Scala Futures类代码示例

本文整理汇总了Scala中com.google.common.util.concurrent.Futures的典型用法代码示例。如果您正苦于以下问题:Scala Futures类的具体用法?Scala Futures怎么用?Scala Futures使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Futures类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
    }
  }
} 
开发者ID:akka,项目名称:alpakka,代码行数:20,代码来源:GuavaFutures.scala

示例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)) }
      }))
    }
  }


} 
开发者ID:Spinoco,项目名称:fs2-cassandra,代码行数:19,代码来源:cassandra.scala

示例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
  }
} 
开发者ID:giampaolotrapasso,项目名称:cassandratest,代码行数:33,代码来源:FutureHelper.scala

示例4: 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)
  }
} 
开发者ID:deaktator,项目名称:advanced-scala-tutorial,代码行数:44,代码来源:ListenableFutureInstances.scala

示例5: 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
    }
  }

} 
开发者ID:prad-a-RuntimeException,项目名称:semantic-store,代码行数:22,代码来源:RichListenableFuture.scala


注:本文中的com.google.common.util.concurrent.Futures类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。