3

我正在尝试使用 Scala 和 spray-client 编写一个简单的 HTTP 客户端。我的客户基于Spray docs上给出的示例。

我的问题是该示例正在创建一个新的隐式ActorSystem 即

implicit val system = ActorSystem()

但我希望我的客户端是可重用的,而不是创建一个新的 ActorSystem。

这是我的代码的要点。

trait WebClient {
  def get(url: String)(implicit system: ActorSystem): Future[String]
}

object SprayWebClient extends WebClient {
  val pipeline: HttpRequest => Future[HttpResponse] = sendReceive

  def get(url: String): Future[String] = {
    val r = pipeline (Get("http://some.url/"))
    r.map(_.entity.asString)
  }

}

但是我收到两个关于隐式的编译器错误

implicit ActorRefFactory required: if outside of an Actor you need an implicit ActorSystem, inside of an actor this should be the implicit ActorContext WebClient.scala ...

not enough arguments for method sendReceive: (implicit refFactory: akka.actor.ActorRefFactory, implicit executionContext: scala.concurrent.ExecutionContext, implicit futureTimeout: akka.util.Timeout)spray.http.HttpRequest => scala.concurrent.Future[spray.http.HttpResponse]. Unspecified value parameters refFactory, executionContext.   WebClient.scala...

我应该如何更改 API 定义?

4

1 回答 1

10

这是一个解决方案:

import akka.actor.ActorSystem
import spray.http.{HttpRequest, HttpResponse}
import scala.concurrent.Future
import spray.client.pipelining._

trait WebClient {
  def get(url: String): Future[String]
}

class SprayWebClient(implicit system: ActorSystem) extends WebClient {
  import system.dispatcher

  val pipeline: HttpRequest => Future[HttpResponse] = sendReceive

  def get(url: String): Future[String] = {
    val r = pipeline (Get("http://some.url/"))
    r.map(_.entity.asString)
  }
}

这是另一个保留原始WebClient.get签名的:

import akka.actor.ActorSystem
import spray.http.{HttpRequest, HttpResponse}
import scala.concurrent.Future
import spray.client.pipelining._

trait WebClient {
  def get(url: String)(implicit system: ActorSystem): Future[String]
}

object SprayWebClient extends WebClient {
  def get(url: String)(implicit system: ActorSystem): Future[String] = {
    import system.dispatcher

    val pipeline: HttpRequest => Future[HttpResponse] = sendReceive
    val r = pipeline (Get("http://some.url/"))
    r.map(_.entity.asString)
  }
}

第二个有点贵,因为管道每次都会重新创建,即使它在理论上是静态的ActorSystem。我更喜欢第一个解决方案,并尝试找到一种方法来传播WebClient通过您的应用程序(通过使用蛋糕模式,通过显式传递它,或通过使用其他依赖注入技术)。

于 2013-12-08T09:35:17.337 回答