Given akka doc I would expect the stream to stop after 7th/8th element. Why is it not stopping ? It continues all the way to the last element (20th).
What I want to achive is that on system terminate, the stream stops requesting new elements and the system to wait termination until all elements in the stream are fully processed (reach the sink)
object StreamKillSwitch extends App {
implicit val system = ActorSystem(Behaviors.ignore, “sks”)
implicit val ec: ExecutionContext = system.executionContext
val (killStream, done) =
Source(1 to 20)
.viaMat(KillSwitches.single)(Keep.right)
.map(i => {
system.log.info(s"Start task $i")
Thread.sleep(100)
system.log.info(s"End task $i")
i
})
.toMat(Sink.foreach(println))(Keep.both)
.run()
CoordinatedShutdown(system)
.addTask(CoordinatedShutdown.PhaseServiceUnbind, “stop-receiving”) {
() => Future(killStream.shutdown()).map(_ => Done)
}
CoordinatedShutdown(system)
.addTask(CoordinatedShutdown.PhaseServiceRequestsDone, “wait-processing-complete”) {
() => done
}
Thread.sleep(720)
system.terminate()
Await.ready(system.whenTerminated, 5.seconds)
}
also on stackoverflow: https://stackoverflow.com/questions/65062099/gracefully-stopping-an-akka-stream/
2 posts - 2 participants