Skip to content

Instantly share code, notes, and snippets.

Embed
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)
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
You can’t perform that action at this time.