您如何处理一系列Akka流源?

问题描述 投票:1回答:1

我们有一个可以处理事件的Sink

def parseEvent(): Sink[T, Future[akka.Done]] = {
  Sink.foreach[T] { event => {
    // Do stuff with the event
  }}
}

这与单个Source正常工作:

val mySource: Source[T] = ...  
mySource.takeWhile( someCheck, true ).runWith(parseEvent)

如果改为使用,怎么办?

val mySources: Seq[Source[T]] = ...

所有源应并行运行,所有事件应达到parseEvent

scala akka akka-stream
1个回答
1
投票

以下内容应符合要求:

import akka.NotUsed
import akka.stream.scaladsl.{ Concat, Merge, Source }

def sourceFromSources[T](sources: Seq[Source[T, NotUsed]]): Source[T, NotUsed] =
  sources.size match {
    case s if s < 1 => Source.empty[T]
    case 1 => sources.head
    case 2 => sources.head.merge(sources(1))
    case _ => Source.combine(sources.head, sources(1), sources(2), sources.drop(2): _*)(Merge(_))
  }

合并策略“合并多个流,在元素从输入流到达时获取元素”,并随机选择多个流是否具有可用元素。背压从下游传播到上游。

© www.soinside.com 2019 - 2024. All rights reserved.