如何突然停止akka流Runnable Graph?

编程入门 行业动态 更新时间:2024-10-11 15:21:41
本文介绍了如何突然停止akka流Runnable Graph?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法弄清楚如何立即停止akka流Runnable Graph?如何使用killswitch实现这一目标?我开始使用akka流已经有几天了。在我的情况下,我正在从文件中读取行并在流程中执行一些操作并写入接收器。我想要做的是,随时随地停止读取文件,我希望这可能会停止整个运行图形。对此有任何想法将不胜感激。

I am not able to figure out how to stop akka stream Runnable Graph immediately ? How to use killswitch to achieve this? It has been just a few days that I started akka streams. In my case I am reading lines from a file and doing some operations in flow and writing to the sink. What I want to do is, stop reading file immediately whenever I want, and I hope this should possibly stop the whole running graph. Any ideas on this would be greatly appreciated.

提前致谢。

推荐答案

从Akka Streams 2.4.3开始,有一种优雅的方法可以通过 KillSwitch 。

Since Akka Streams 2.4.3, there is an elegant way to stop the stream from the outside via KillSwitch.

请考虑以下示例,该示例在10秒后停止流。

Consider the following example, which stops stream after 10 seconds.

object ExampleStopStream extends App { implicit val system = ActorSystem("streams") implicit val materializer = ActorMaterializer() import system.dispatcher val source = Source. fromIterator(() => Iterator.continually(Random.nextInt(100))). delay(500.millis, DelayOverflowStrategy.dropHead) val square = Flow[Int].map(x => x * x) val sink = Sink.foreach(println) val (killSwitch, done) = source.via(square). viaMat(KillSwitches.single)(Keep.right). toMat(sink)(Keep.both).run() system.scheduler.scheduleOnce(10.seconds) { println("Shutting down...") killSwitch.shutdown() } done.foreach { _ => println("I'm done") Await.result(system.terminate(), 1.seconds) } }

更多推荐

如何突然停止akka流Runnable Graph?

本文发布于:2023-11-25 11:07:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629499.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:akka   Runnable   Graph

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!