Akka流为多个http请求

时间:2016-12-30 14:16:23

标签: scala akka akka-stream akka-http

在我的一个项目中,我有一个akka演员用于向我的谷歌fcm服务器发送帖子请求。 actor会获取一个id列表,并且应该生成与列表中一样多的请求。我在runForeach(println(_))打印出来自服务器的响应,但我只得到一个打印输出以获得整个ID列表。为什么会这样?

class FCMActor(val key: String) extends Actor{
  import fcm.FCMActor._
  import akka.pattern.pipe
  import context.dispatcher

  private implicit def system: ActorSystem = ActorSystem()
  final implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system))

  def buildBody(id: Option[String]): String = {
    Json.obj(
      "to" -> id,
      "priority" -> "high",
      "data" -> Json.obj("message" -> "Firebase Clud Message"),
      "time_to_live" -> 60
    ).toString()
  }

  def buildHttpRequest(body: String): HttpRequest = {
    HttpRequest(method = HttpMethods.POST,
      uri = s"/fcm/send",
      entity = HttpEntity(MediaTypes.`application/json`, body),
      headers = List(RawHeader("Authorization", s"key=$key")))
  }

  val connectionFlow: Flow[HttpRequest, HttpResponse, Future[Http.OutgoingConnection]] = {
    Http().outgoingConnection("fcm.googleapis.com")
  }

  def send(ids: List[Option[String]]) = {

    val httpRequests: List[HttpRequest] = ids.map(buildBody).map(buildHttpRequest)
    println(httpRequests)

    Source(httpRequests).via(connectionFlow).runForeach(println(_)) // << here I only get one println
  }

  override def receive: Receive = {
    case SendToIds(ids: List[Option[String]]) =>
      send(ids)

  }
}

1 个答案:

答案 0 :(得分:3)

您没有消耗服务器发送给您的响应实体。要了解其重要性,请查看相关的docs page

尝试修复此问题的快速代码更改是:

... .runForeach{ response =>
  response.discardEntityBytes()
  println(response)
}

或者,如果你真的对这个实体感兴趣,那就是

... .runForeach{ _.entity.dataBytes
  .runFold(ByteString.empty) { case (acc, b) => acc ++ b }
  .map(println(_))
}