3
我有一個問題與AKK-HTTP。我試圖在一個流程中多次請求,但是它以4次的默認配置停止。這是我使用的代碼。有人能幫我理解爲什麼它在等待嗎?爲什麼akka http不會繼續請求?
非常感謝
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.Http.{HostConnectionPool, OutgoingConnection}
import akka.http.scaladsl.model.{HttpRequest, HttpResponse}
import akka.stream._
import akka.stream.scaladsl._
import com.typesafe.scalalogging.LazyLogging
import scala.concurrent.Future
import scala.util.{Try, Failure, Success}
object TestHttp extends LazyLogging {
def main(args: Array[String]) {
implicit val system = ActorSystem()
import system.dispatcher
val decider: Supervision.Decider = {
case e => {
logger.error(e.getMessage, e)
Supervision.Resume
}
}
implicit val materializer = ActorMaterializer(ActorMaterializerSettings(system).withSupervisionStrategy(decider).withDebugLogging(true))
val resultSink: Sink[(Try[HttpResponse], Int), Future[Unit]] = Sink.foreach { case (hr, i) => {
hr match {
case Success(r) => logger.info(s"Success ${r} for ${i}")
case Failure(e) => logger.error(s"Error ${e} for ${i}")
}
}
}
val source: Source[Int, Unit] = Source(0 to 1500).map(i => {
logger.info(s"${i} iteration")
i
})
val buildHr: Flow[Int, (HttpRequest, Int), Unit] = Flow[Int].map { case s => {
(HttpRequest(uri = "/").withDefaultHeaders(), s)
}
}
val connection: Flow[(HttpRequest, Int), (Try[HttpResponse], Int), HostConnectionPool] = Http().cachedHostConnectionPool("www.adajam.uk.com")
import FlowGraph.Implicits._
source.via(buildHr).via(connection).to(resultSink).run()
}
}
您需要閱讀響應主體,即使用'r.entity.dataBytes'做某些事情。否則,連接不能重新使用。 – jrudolph
謝謝jrudolph,它的工作原理。讀取實體後,流程繼續並釋放連接。 –
酷,很高興它的作品! :) – jrudolph