2015-10-23 59 views
6

我有這個兩個誤差修改:找不到隱式的ExecutionContext。你可能會通過噴霧斯卡拉

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() 
} 
} 

}` 

回答

20

你需要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) 

它使用默認工作盜取池。所以您可能需要爲不同類型的併發需求提供不同類型的執行上下文。

+0

你的回答是正確的,但@Fatih Donmez我想在我didi log.info(「海拔Mt.Everest是:{} m」,result.toString()。decodeOption.get)但是所有的時間我有錯誤:錯誤:(54,76)發散隱式擴展的類型argonaut.DecodeJson [A] 以特性中的方法SetDecodeJson開始DecodeJsons log.info(「Mt.Everest的高程爲:{} m」,result .toString()。decodeOption.get) ^ –

+0

我認爲你應該爲它創建另一個問題。 –

+0

我不能達到我的問題限制:':(:((( –