Last active
March 22, 2017 17:07
-
-
Save kovacshuni/ea842aa4546e2f74354068e86eed073a to your computer and use it in GitHub Desktop.
scala-akka-streams-concurrent-requests
This file contains 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
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