【发布时间】:2016-12-30 18:12:31
【问题描述】:
我尝试关闭转换一些数字的流并创建相同图形蓝图的另一个流。但是,第二个流实例没有运行,或者至少它没有向控制台打印任何内容。
我错过了什么?
object KillSwitchSample extends App {
implicit val actorSystem = ActorSystem()
implicit val materializer = ActorMaterializer()
val killSwitch = KillSwitches.shared("switch")
val stream1 = createStream("stream 1")
stream1.run()
Thread.sleep(200)
killSwitch.shutdown()
val stream2 = createStream("stream 2")
stream2.run()
Thread.sleep(200)
killSwitch.shutdown()
def createStream(streamName: String): RunnableGraph[NotUsed] = {
Source.fromGraph(new NumbersSource)
.via(killSwitch.flow)
.map(el => s"$streamName: $el")
.to(Sink.foreach(println))
}
}
class NumbersSource extends GraphStage[SourceShape[Int]] {
val out: Outlet[Int] = Outlet("NumbersSource")
override val shape: SourceShape[Int] = SourceShape(out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic =
new GraphStageLogic(shape) {
private var counter = 1
setHandler(out, new OutHandler {
override def onPull(): Unit = {
push(out, counter)
counter += 1
}
})
}
}
【问题讨论】:
标签: scala akka akka-stream