簡體   English   中英

使用 Akka Streams 的一個源到多個接收器

[英]One Source to Many Sink using Akka Streams

我正在使用 Akka Streams 為我已經使用 Rx Scala 的項目之一嘗試一些東西。 我很想看看 Akka Streams 如何適合替換我們擁有的 Rx Scala 庫。 我認為 Akka Streams 無法實現的一件事是擁有一個源和多個接收器的可能性。 比如說,在這個直接取自 Akka Streams 文檔的示例中:

val source = Source(1 to 10)
val sink = Sink.fold[Int, Int](0)(_ + _)

// connect the Source to the Sink, obtaining a RunnableGraph
val runnable: RunnableGraph[Future[Int]] = source.toMat(sink)(Keep.right) // how could I materialize to a Seq of Sinks?

// materialize the flow and get the value of the FoldSink
val sum: Future[Int] = runnable.run()

使用 Rx 庫時,我將 Source(Observable)和 Sink(Observer)完全解耦,這讓我可以靈活地映射 1 個 Source(Observable)和 n 個 Sinks(Observer)。 如何使用 Akka Streams 實現這一目標? 任何指針都會有所幫助!

這可用於Graphs ,特別是Broadcast

Broadcast[T] –(1 個輸入,N 個輸出)給定一個輸入元素向每個輸出發出

文檔中的一些示例代碼:

val in = Source(1 to 10)
val out = Sink.ignore

val bcast = builder.add(Broadcast[Int](2))
val merge = builder.add(Merge[Int](2))

val f1, f2, f3, f4 = Flow[Int].map(_ + 10)

in ~> f1 ~> bcast ~> f2 ~> merge ~> f3 ~> out
            bcast ~> f4 ~> merge
ClosedShape

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM