• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala ExecutionException类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



在下文中一共展示了ExecutionException类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: RestartableSpec

//设置package包名称以及导入依赖的类
package httpmock

import java.util.concurrent.ExecutionException

class RestartableSpec extends TestHelper {
  describe("HttpMock") {
    val url: String = s"http://127.0.0.1:$testPort"
    def ok(): Unit = assert(get(url).getStatusCode === 200)
    def ng(): Unit = intercept[ExecutionException] { get(url) }

    it("can stop and start") {
      val started: HttpMock = autoStop(HttpMock.start(testPort))
      ok()

      val stopped: HttpMock = started.stop
      ng()

      val restarted: HttpMock = autoStop(stopped.start)
      ok()
    }
  }
} 
开发者ID:xuwei-k,项目名称:httpmock,代码行数:23,代码来源:RestartableSpec.scala


示例2: RunnableSpec

//设置package包名称以及导入依赖的类
package httpmock

import java.util.concurrent.ExecutionException

class RunnableSpec extends TestHelper {
  val url: String = s"http://127.0.0.1:$testPort"
  def ok(): Unit = assert(get(url).getStatusCode === 200)
  def ng(): Unit = intercept[ExecutionException] { get(url) }

  describe("Setting()") {
    it("run in loan pattern") {
      Setting(port = testPort).run { server =>
        ok()
      }
      ng()
    }
  }

  describe("HttpMock") {
    it("run(port) in loan pattern") {
      HttpMock.run(testPort) { server =>
        ok()
      }
      ng()
    }
  }
} 
开发者ID:xuwei-k,项目名称:httpmock,代码行数:28,代码来源:RunnableSpec.scala


示例3: GuavaFutureTest

//设置package包名称以及导入依赖的类
package example

import java.util.concurrent.ExecutionException
import com.gilt.gfc.guava.future.FutureConverters._
import com.google.common.util.concurrent.SettableFuture
import scala.concurrent.Future
import org.scalatest.{Matchers, FlatSpec}
import org.scalatest.concurrent.ScalaFutures._

class GuavaFutureTest extends FlatSpec with Matchers {

  "Guava ListenableFuture conversion" should "work" in {
    val guavaFuture = SettableFuture.create[String]
    guavaFuture.set("Hello world")

    val future = guavaFuture.asScala
    future.isCompleted should be (true)
    future.futureValue should be ("Hello world")
    future.asListenableFuture.get should be ("Hello world")

    val failedFuture = Future.failed[String](new IllegalStateException("OMG WTF"))
    failedFuture.isCompleted should be (true)

    val failedGuavaFuture = failedFuture.asListenableFuture
    failedGuavaFuture.isDone should be (true)
    val cause = intercept[ExecutionException] { failedGuavaFuture.get }.getCause
    cause.isInstanceOf[IllegalStateException] should be (true)
    cause.getMessage should be ("OMG WTF")
  }

} 
开发者ID:sullis,项目名称:java-scala-interop-examples,代码行数:32,代码来源:GuavaFutureTest.scala


示例4: ScalaAsyncLoadingCache

//设置package包名称以及导入依赖的类
package com.evolutiongaming.scaffeine

import java.util.concurrent.ExecutionException

import com.evolutiongaming.concurrent.{CurrentThreadExecutionContext, ExecutionContextExecutorServiceFactory}
import com.github.benmanes.caffeine.cache.{AsyncLoadingCache => CaffeineAsyncLoadingCache}
import com.github.blemale.scaffeine.{AsyncLoadingCache, Scaffeine}

import scala.compat.java8.FutureConverters._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Try}

class ScalaAsyncLoadingCache[K, V](
  underlying: CaffeineAsyncLoadingCache[K, V],
  statsCounter: Option[MetricsStatsCounter] = None) extends AsyncLoadingCache(underlying) {

  def getFuture(key: K): Future[Option[V]] = {
    val cf = underlying get key
    if (cf.isDone) {
      Future fromTry Try(Option(cf.get())).recoverWith { case e: ExecutionException => Failure(e.getCause) }
    } else {
      (cf.toScala map Option.apply) (CurrentThreadExecutionContext)
    }
  }

  def getBlocking(key: K): Option[V] = {
    val futureValue = underlying get key
    if (!futureValue.isDone) statsCounter foreach { _.recordBlockingCall() }
    Option(futureValue.get())
  }
}

object ScalaAsyncLoadingCache {
  def apply[K, V](
    scaffeine: Scaffeine[Any, Any],
    loader: K => Future[Option[V]],
    statsCounter: Option[MetricsStatsCounter])
    (implicit ec: ExecutionContext): ScalaAsyncLoadingCache[K, V] = {

    val l = loader andThen { _.map { _ getOrElse null.asInstanceOf[V] }(CurrentThreadExecutionContext) }

    val executorService = ExecutionContextExecutorServiceFactory(ec)
    val cache = scaffeine.executor(executorService).buildAsyncFuture[K, V](l).underlying
    new ScalaAsyncLoadingCache[K, V](cache, statsCounter)
  }
} 
开发者ID:evolution-gaming,项目名称:metered-scaffeine,代码行数:47,代码来源:ScalaAsyncLoadingCache.scala


示例5: ShapeShiftGigahorseProvider

//设置package包名称以及导入依赖的类
package com.alexdupre.shapeshift.providers

import java.nio.charset.Charset
import java.util.concurrent.ExecutionException

import com.alexdupre.shapeshift.{BuildInfo, ShapeShiftClient}
import gigahorse._
import gigahorse.support.okhttp.Gigahorse
import play.api.libs.json.{JsValue, Json}

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._

class ShapeShiftGigahorseProvider(http: HttpClient)(implicit ec: ExecutionContext) extends ProviderAPI {

  override def get(endpoint: String): Future[ProviderResponse] = {
    val req = Gigahorse.url(endpoint).get
    execute(req)
  }

  override def post(endpoint: String, params: JsValue): Future[ProviderResponse] = {
    val payload = Json.stringify(params)
    val utf8    = Charset.forName("UTF-8")
    val req     = Gigahorse.url(endpoint).withContentType("application/json", utf8).post(payload, utf8)
    execute(req)
  }

  private def execute(req: Request): Future[ProviderResponse] = {
    http.run(req) map { r =>
      val js = try {
        Json.parse(r.bodyAsString)
      } catch {
        case e: Exception => sys.error("ShapeShift Protocol Exception")
      }
      ProviderResponse(r.status, js)
    } recover {
      case e: ExecutionException => throw e.getCause
    }
  }
}

object ShapeShiftGigahorseProvider {

  def newClient(http: HttpClient = defaultHttp)(implicit ec: ExecutionContext) =
    new ShapeShiftClient(new ShapeShiftGigahorseProvider(defaultHttp))

  lazy val defaultHttp = {
    val config = Gigahorse.config
      .withUserAgentOpt(Some(s"AlexDupre-ShapeShift/${BuildInfo.version}"))
      .withConnectTimeout(5 seconds)
      .withReadTimeout(30 seconds)
      .withRequestTimeout(60 seconds)
    Gigahorse.http(config)
  }

} 
开发者ID:alexdupre,项目名称:shapeshift-scala,代码行数:57,代码来源:ShapeShiftGigahorseProvider.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala JSONObject类代码示例发布时间:2022-05-23
下一篇:
Scala Iterable类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap