组合Source时保留实现类型

时间:2017-10-16 07:33:54

标签: scala akka-stream

我希望在实现后合并2个akka流源并保留第一个来源的ActorRef

val buffer = 100

val apiSource: Source[Data, ActorRef] = Source.actorRef[Data](buffer, OverflowStrategy.backpressure)
  .delay(2.second, DelayOverflowStrategy.backpressure)

val kafkaSource:   Source[Data, Consumer.Control] = createConsumer(config.kafkaConsumerConfig, "test")

val combinedSource: Source[Data, NotUsed] = Source.combine(kafkaSource, apiSource)(Merge(_))

问题是combined方法忽略了物化类型,我想知道是否有其他方法可以实现这个

2 个答案:

答案 0 :(得分:1)

您可以使用Source#mergeMat

val combinedSource: Source[Data, ActorRef] = kafkaSource.mergeMat(apiSource)(Keep.right)

答案 1 :(得分:0)

这似乎有效

    def combineAndRetainFirst[T,M1, M2](first: Source[T, M1], second: Source[T, M2]): Source[T, M1] ={
    Source.fromGraph(
      GraphDSL.create(first, second)((m1, _) => m1){ implicit builder => (g1, g2) =>
        import GraphDSL.Implicits._

        val merge = builder.add(Merge[T](2))

        g1 ~> merge.in(0)
        g2 ~> merge.in(1)

        SourceShape(merge.out)
      }
    )
  }