Akka Stream - 并行可运行图

时间:2016-04-27 02:12:23

标签: akka akka-stream

需要建议,我需要运行并行多个源图,例如我创建了这个示例代码,我创建10个图并并行运行它们。

这是正确的方法,还是应该在图表中创建多个源并在一个图表中并行运行?

display: inline-flex

由于 阿伦

1 个答案:

答案 0 :(得分:0)

我使用http://doc.akka.io/docs/akka/2.4.4/scala/stream/stream-parallelism.html尝试了并行性,这看起来不错,我的源不同但是流和接收器是相同的。每个源都是下面的示例中的模拟,考虑它们就像你从一些外部源读取流:

object TestParallelGraph extends App {

  implicit val system = ActorSystem("test")
  implicit val dispacher = system.dispatcher
  implicit val materializer = ActorMaterializer()

  val listOfDifferentSource=List(1,2,3) //consider we have to read data from various sources


 def createGraph() = {
    RunnableGraph.fromGraph(GraphDSL.create() {
      implicit builder =>
        import GraphDSL.Implicits._

        val merge=builder.add(Merge[Int](listOfDifferentSource.length))

        val flow=builder.add(Flow[Int].map(_ + 10)) //just random flow to add 10

        //again as mentioned above creating source with different information to simulate
        Source(listOfDifferentSource.head*100 to 100* listOfDifferentSource.head+10) ~> merge ~> flow ~> Sink.foreach(println)

        for{
          i <- listOfDifferentSource.tail //for each other source
        }yield (Source(100*i to 100*i+10) ~> merge) 

        ClosedShape
    })
  }

  createGraph().run()
}