2015-10-23 7 views
6

पारित हो सकता है मैं इस दो मिली त्रुटियाँ हैं:एक निहित निष्पादन कॉन्टेक्स्ट नहीं मिला। आप स्प्रे स्केला

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 पैरामीटर देखते हैं।

--edit--

इस उत्तर हो रही है दृश्य के बहुत सारे तो मैं इसे अपडेट करना। जैसा कि आप देखते हैं कि यह डिफ़ॉल्ट वैश्विक निष्पादन संदर्भ है, जैसा घोषित किया गया है;

/** 
* 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 ("माउंट.इवरेस्ट की ऊंचाई है: {} एम", result.toString()। DecodeOption.get) लेकिन हर समय उत्खनन करना चाहता हूं मुझे त्रुटि है: त्रुटि: (54, 76) टाइप Argonaut.DecodeJson [ए] विधि के साथ शुरू करने के तरीके से सेट डीकोडजेसन log.info ("माउंट एवरेस्ट की ऊंचाई है: {} एम", परिणाम .toString() decodeOption.get) ^ –

+0

मुझे लगता है कि आपको इसके लिए एक और प्रश्न बनाना चाहिए। –

+0

मैं अपनी प्रश्न सीमा तक नहीं पहुंच सकता: '(:( –

संबंधित मुद्दे