使用Akka Streams的许多接收器的一个来源

时间:2016-04-26 17:27:23

标签: rx-java akka-stream

我正在尝试使用Akka Streams的一些东西用于我已经使用Rx Scala的项目之一。我很想看看Akka Streams如何适合替换我们拥有的Rx Scala库。我认为Akka Streams不可能的一件事是有一个Source和许多Sinks的可能性。比如说,在这个例子中直接从Akka Streams文档中获取:

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
        <!-- Accent and AppTheme setting -->
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
        <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
      </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>

当使用Rx库时,我将Source(Observable)和Sink(Observer)完全解耦,这使我可以灵活地映射1个Source(Observable)并有n个Sinks(观察者)。我怎样才能通过Akka Streams实现这一目标?任何指针都会有所帮助!

1 个答案:

答案 0 :(得分:2)

这适用于Graphs,具体为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