找不到隐式 ExecutionContext。你可能会通过 spray scala
Posted
技术标签:
【中文标题】找不到隐式 ExecutionContext。你可能会通过 spray scala【英文标题】:Cannot find an implicit ExecutionContext. You might pass spray scala 【发布时间】:2016-01-23 02:10:48 【问题描述】:我有这两个错误:
Error:(39, 20) Cannot find an implicit ExecutionContext. You might pass
an (implicit ec: ExecutionContext) parameter to your method
or import scala.concurrent.ExecutionContext.Implicits.global.
val pipeline = sendReceive
^
Error:(39, 20) not enough arguments for method sendReceive: (implicit refFactory: akka.actor.ActorRefFactory, implicit executionContext: scala.concurrent.ExecutionContext, implicit futureTimeout: akka.util.Timeout)spray.client.pipelining.SendReceive.
Unspecified value parameter executionContext.
val pipeline = sendReceive
^
我的代码是:
import scala.util.Success, Failure
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
import akka.io.IO
import spray.can.Http
import spray.client.pipelining._
import spray.util._
import argonaut._, Argonaut._
object test
case class Elevation(location: Location, elevation: Double)
case class Location(lat: Double, lng: Double)
case class GoogleApiResult(status: String, results: List[Elevation])
implicit def locationFormat: CodecJson[Location] = casecodec2(Location.apply, Location.unapply)("lat", "lng")
implicit def elevationFormat: CodecJson[Elevation] = casecodec2(Elevation.apply, Elevation.unapply)("location", "elevation")
implicit def googleApiResultFormat: CodecJson[GoogleApiResult] = casecodec2(GoogleApiResult.apply, GoogleApiResult.unapply)("status", "results")
object Main extends App
// we need an ActorSystem to host our application in
implicit val system = ActorSystem("simple-spray-client")
// execution context for futures below
val log = Logging(system, getClass)
log.info("Requesting the elevation of Mt. Everest from Googles Elevation API...")
val pipeline = sendReceive
val responseFuture= pipeline
Get("http://maps.googleapis.com/maps/api/elevation/json?locations=27.988056,86.925278&sensor=false")
responseFuture.onComplete
case Success(result) =>
println(result)
log.info("The elevation of Mt.Everest is: m", result.toString.decodeOption[Elevation].get)
shutdown()
case Failure(error) =>
log.error(error, "Couldn't get elevation")
shutdown()
def shutdown(): Unit =
IO(Http).ask(Http.CloseAll)(1.second).await
system.shutdown()
`
【问题讨论】:
【参考方案1】:您需要import scala.concurrent.ExecutionContext.Implicits.global
作为错误指定。
你看sendReceive
方法有implicit executionContext: scala.concurrent.ExecutionContext
参数。
--编辑--
这个答案得到了很多关注,所以我想更新它。如您所见,这是默认的全局执行上下文,声明如下;
/**
* The implicit global `ExecutionContext`. Import `global` when you want to provide the global
* `ExecutionContext` implicitly.
*
* The default `ExecutionContext` implementation is backed by a work-stealing thread pool. By default,
* the thread pool uses a target number of worker threads equal to the number of
* [[https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#availableProcessors-- available processors]].
*/
implicit lazy val global: ExecutionContextExecutor = impl.ExecutionContextImpl.fromExecutor(null: Executor)
它使用默认的工作窃取池。因此,您可能需要为不同类型的并发需求提供不同类型的执行上下文。
【讨论】:
您的回答是正确的,但是@Fatih Donmez 我想在我滴滴 log.info("珠穆朗玛峰的海拔高度是: m", result.toString().decodeOption.get ) 但我一直有错误:错误:(54, 76) 类型 argonaut.DecodeJson[A] 的发散隐式扩展从特征 DecodeJsons log.info 中的方法 SetDecodeJson 开始(“珠穆朗玛峰的海拔是: m ", result.toString().decodeOption.get) ^ 我认为你应该为它创建另一个问题。 我无法达到我的问题限制:'( :( 我不知道第二个问题。这就是为什么我要求你创建一个新的。因为其他人可以帮助您解决更详细的新问题。如果我的回答解决了您的主要问题,请批准。 你能发布解决方案吗?以上是关于找不到隐式 ExecutionContext。你可能会通过 spray scala的主要内容,如果未能解决你的问题,请参考以下文章