Skip to content

Instantly share code, notes, and snippets.

@loicdescotte
Last active November 6, 2021 00:41
Show Gist options
  • Save loicdescotte/5f3ed7a56b8d3fa8eb2982e9e97dcb36 to your computer and use it in GitHub Desktop.
Save loicdescotte/5f3ed7a56b8d3fa8eb2982e9e97dcb36 to your computer and use it in GitHub Desktop.
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