Last active
November 6, 2021 00:41
-
-
Save loicdescotte/5f3ed7a56b8d3fa8eb2982e9e97dcb36 to your computer and use it in GitHub Desktop.
Play Framework Dynamic Akka Streams Source
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
def streamWithQueue = Action { | |
implicit val myExecutionContext: ExecutionContext = actorSystem.dispatchers.lookup("queue-context") | |
val (queue: SourceQueueWithComplete[String], source: Source[String, NotUsed]) = { | |
Source.queue[String](100, OverflowStrategy.backpressure).preMaterialize | |
} | |
// simulate a background process feeding the queue | |
Future.traverse((1 to 1000).toList) { i => | |
// sleep just for the example, to see numbers be displayed one by one in streaming | |
Future(Thread.sleep(1000)).flatMap { _ => | |
println("add " + i) | |
queue.offer(i.toString) | |
} | |
}.map(_ => queue.complete()) | |
Ok.chunked(source) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment