Akka http 请求在通过管道传输给 actor 时不会被 runFold 使用

Akka http request is not consumed with runFold when it's piped to actor

我的代码基于 Akka 文档中的示例,因此它应该可以正常工作,但问题是我只收到 headers 的响应、内容长度和折叠 [=21] =] 我得到:

[INFO] [08/17/2016 13:01:21.116] [default-akka.actor.default-dispatcher-9] [akka://default/user/$a] Length: 29407

[INFO] [08/17/2016 13:01:21.127] [default-akka.actor.default-dispatcher-9] [akka://default/user/$a] Got response, body: List()

演员代码:

class AkkaHttp extends Actor with ActorLogging {

  import akka.pattern.pipe
  import context.dispatcher

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

  val http = Http(context.system)

  override def preStart() = {
    http.singleRequest(HttpRequest(uri = "http://akka.io/"))
      .pipeTo(self)
  }

  def receive = {
    case s: String => {
      log.info(s)
    }
    case HttpResponse(StatusCodes.OK, headers, entity, _) => {
      log.info("Length: " + entity.contentLengthOption.getOrElse("No content"))
      log.info("Got response, body: " + entity.dataBytes.runFold(ByteString.empty) {
        case (acc, b) => acc ++ b }.map(s => s.utf8String))
    }
    case HttpResponse(code, _, _, _) =>
      log.info("Request failed, response code: " + code)
  }

}

如果我更改 preStart() 方法以在其中折叠响应并仅发送字符串,则会记录页面的 body:

  override def preStart() = {
    val httpRequest: HttpRequest = HttpRequest(uri = "http://akka.io/")
    val request = http.singleRequest(httpRequest)
    request.flatMap { response =>
      response.entity.dataBytes.runFold(ByteString.empty) { 
          case (acc, b) => acc ++ b }.map(f => f.utf8String)
    }
    .pipeTo(self)
  }

第一个版本中没有折叠响应实体的原因是什么?

runFold 的结果是另一个 Future。然后你去尝试打印 Future,它不会产生任何有价值的东西,因为它是 Future 而不是你想要的基础 String。如果您将 receive 中的大小写更改为此,那么它应该开始工作:

case HttpResponse(StatusCodes.OK, headers, entity, _) => 
  log.info("Length: " + entity.contentLengthOption.getOrElse("No content"))

  val entityFut = 
    entity.dataBytes.runFold(ByteString.empty) {
      case (acc, b) => acc ++ b 
    }.
    map(s => s.utf8String)
  entityFut pipeTo self