我遇到了akka-http的问题。我试图在流程中多次请求,但它在默认配置下停止4次。这是我使用的代码。有人能帮我理解为什么会等吗?
非常感谢
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()
}
}
答案 0 :(得分:3)
为了释放连接,必须阅读正文。 这可以通过
完成r.entity.dataBytes.to(Sink.ignore)