Skip to content

Instantly share code, notes, and snippets.

@kovacshuni
Last active March 22, 2017 17: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 kovacshuni/ea842aa4546e2f74354068e86eed073a to your computer and use it in GitHub Desktop.
Save kovacshuni/ea842aa4546e2f74354068e86eed073a to your computer and use it in GitHub Desktop.
scala-akka-streams-concurrent-requests
Source(List("/", "/docs"))
.map(theUri => HttpRequest(uri = theUri))
.via(Http().outgoingConnection("akka.io").async)
.mapAsyncUnordered(2)(r => r.entity.toStrict(2 seconds))
.map(e => e.data.utf8String)
.fold(List[String]())(_ :+ _)
.runWith(Sink.head)
.onComplete(logger.info("{}", _))
//other
Http().singleRequest(HttpRequest(uri = "http://akka.io")).flatMap { response1 =>
Http().singleRequest(HttpRequest(uri = "http://scala-lang.org")).flatMap { response2 =>
response1.entity.toStrict(1 second).flatMap { entity1 =>
response2.entity.toStrict(1 second).map { entity2 =>
entity1.data.utf8String + entity2.data.utf8String
}
}
}
}.onComplete(result => logger.info("{}", result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment