Skip to content

Instantly share code, notes, and snippets.

@note
Created September 25, 2017 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save note/b7071b4555549919adf6cdf26c61cce8 to your computer and use it in GitHub Desktop.
Save note/b7071b4555549919adf6cdf26c61cce8 to your computer and use it in GitHub Desktop.
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