Created
September 25, 2017 12:07
-
-
Save note/b7071b4555549919adf6cdf26c61cce8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorSystem | |
import akka.http.scaladsl.Http | |
import akka.http.scaladsl.Http.HostConnectionPool | |
import akka.http.scaladsl.model.{HttpRequest, HttpResponse} | |
import akka.stream.ActorMaterializer | |
import akka.stream.scaladsl.{Flow, Sink, Source} | |
import scala.util.{Failure, Success, Try} | |
object Main { | |
def main(args: Array[String]): Unit = { | |
implicit val system = ActorSystem() | |
implicit val materializer = ActorMaterializer() | |
implicit val ec = system.dispatcher | |
val poolClientFlow: Flow[(HttpRequest, Int), (Try[HttpResponse], Int), HostConnectionPool] = | |
Http().cachedHostConnectionPoolHttps[Int]("www.scala-lang.org", 443, Http().defaultClientHttpsContext) | |
val requests = List.fill(100)(HttpRequest(uri = "/")).zipWithIndex | |
val source = Source(requests) | |
val responses = source | |
.via(poolClientFlow) | |
.map { r => | |
println("Single response: " + r) | |
r._1.map(_.discardEntityBytes()) | |
r | |
} | |
.map(r => (Failure(MyException("hardcoded exception")), r._2)) | |
.runWith(Sink.seq) | |
responses.onComplete { | |
case Success(res) => | |
println("Completed all with success: " + res) | |
case Failure(ex) => | |
println("Failure: " + ex) | |
} | |
} | |
} | |
case class MyException(msg: String) extends Exception(msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment