14

I have this two erros:

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
               ^

My Code is:

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

}`
tuxdna
  • 8,257
  • 4
  • 43
  • 61
kam kimo
  • 193
  • 1
  • 2
  • 7

1 Answers1

33

You need to import scala.concurrent.ExecutionContext.Implicits.global as error specify.

You see sendReceive method has implicit executionContext: scala.concurrent.ExecutionContext parameter.

--edit--

This answer is getting lots of view so I want to update it. As you see this is default global execution context, declared like;

/**
 * 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)

It uses default work stealing pool. So you may need to provide different kind of execution context for different kind of concurrency need.

Fatih Donmez
  • 4,319
  • 3
  • 33
  • 45
  • you response is correct but @Fatih Donmez I want to evation when I didi log.info("The elevation of Mt.Everest is: {} m", result.toString().decodeOption.get) but all the time I have error: Error:(54, 76) diverging implicit expansion for type argonaut.DecodeJson[A] starting with method SetDecodeJson in trait DecodeJsons log.info("The elevation of Mt.Everest is: {} m", result.toString().decodeOption.get) ^ – kam kimo Oct 23 '15 at 14:55
  • I think you should create another question for it. – Fatih Donmez Oct 23 '15 at 14:57
  • I can't I have reached my question limit :'( :( – kam kimo Oct 23 '15 at 15:02
  • I have no idea for this second question. That's why I asked you to create new one. Because someone else can help you with more detailed new question. If my answer solves your main question please approve it. – Fatih Donmez Oct 23 '15 at 16:50
  • 1
    can you post the solution ? – SaKou Feb 18 '16 at 15:39
  • What is impl exactly here? – Seraf Feb 19 '18 at 17:40
  • It's the package name. – Fatih Donmez Feb 20 '18 at 04:22