本文整理汇总了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()
}
}
}
示例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()
}
}
}
示例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")
}
}
示例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)
}
}
示例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)
}
}