val sharedKillSwitch = KillSwitches.shared("multi-ks") val source2 = Source(Stream.from(1)).delay(2.second,DelayOverflowStrategy.backpressure) source2.via(sharedKillSwitch.flow).to(sink).run() source.via(sharedKillSwitch.flow).to(sink).run() scala.io.StdIn.readLine() killSwitch.shutdown() sharedKillSwitch.shutdown()
import akka.stream.scaladsl._ import akka.stream._ import akka.actor._ import scala.concurrent.duration._ object KillSwitchDemo extends App { implicit val actorSys = ActorSystem("sys") implicit val ec = actorSys.dispatcher implicit val mat = ActorMaterializer( ActorMaterializerSettings(actorSys) .withInputBuffer(16,16) ) val source = Source(Stream.from(1,2)).delay(1.second,DelayOverflowStrategy.backpressure) val sink = Sink.foreach(println) val killSwitch = source.viaMat(KillSwitches.single)(Keep.right).to(sink).run() val sharedKillSwitch = KillSwitches.shared("multi-ks") val source2 = Source(Stream.from(1)).delay(2.second,DelayOverflowStrategy.backpressure) source2.via(sharedKillSwitch.flow).to(sink).run() source.via(sharedKillSwitch.flow).to(sink).run() scala.io.StdIn.readLine() killSwitch.shutdown() sharedKillSwitch.shutdown() println("terminated!") actorSys.terminate() }