Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Play Framework Dynamic Akka Streams Source
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)
}.map(_ => queue.complete())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment