Akka Streams:如何从GraphDSL API获得Materialized Sink输出?

时间:2017-02-23 21:43:21

标签: scala akka-stream

使用GraphDSL API这是一个非常简单的新手问题。我读了几个相关的SO线程,但我没有看到答案:

val actorSystem = ActorSystem("QuickStart")
val executor = actorSystem.dispatcher
val materializer = ActorMaterializer()(actorSystem)

val source: Source[Int, NotUsed] = Source(1 to 5)
val throttledSource = source.throttle(1, 1.second, 1, ThrottleMode.shaping)
val intDoublerFlow = Flow.fromFunction[Int, Int](i => i * 2)
val sink = Sink.foreach(println)

val graphModel = GraphDSL.create() { implicit b =>
  import GraphDSL.Implicits._

  throttledSource ~> intDoublerFlow ~> sink

  // I presume I want to change this shape to something else
  // but I can't figure out what it is.
  ClosedShape
}
// TODO: This is RunnableGraph[NotUsed], I want RunnableGraph[Future[Done]] that gives the
// materialized Future[Done] from the sink. I presume I need to use a GraphDSL SourceShape
// but I can't get that working.
val graph = RunnableGraph.fromGraph(graphModel)

// This works and gives me the materialized sink output using the simpler API.
// But I want to use the GraphDSL so that I can add branches or junctures.
val graphThatIWantFromDslAPI = throttledSource.toMat(sink)(Keep.right)

0 个答案:

没有答案