如何定义AmqpSource订阅多个交换?

时间:2018-05-04 04:55:11

标签: scala akka alpakka

现在我正在使用

订阅单笔交易
AmqpSource.atMostOnceSource(
    NamedQueueSourceSettings(..))

我希望能够订阅多个交易所。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:1)

如果对于特定的alpakka源没有特定的内容,您可以使用Merge或MerbeHub。

如果您事先了解所有来源,则可以使用Merge

将多个来源合并为一个来源

如果您事先不知道所有来源,可以使用MergeHub例如

// A simple consumer that will print to the console for now  
val consumer = Sink.foreach(println)

// Attach a MergeHub Source to the consumer. This will materialize to a
// corresponding Sink.
val runnableGraph: RunnableGraph[Sink[String, NotUsed]] =
  MergeHub.source[String](perProducerBufferSize = 16).to(consumer)

// By running/materializing the consumer we get back a Sink, and hence
// now have access to feed elements into it. This Sink can be materialized
// any number of times, and every element that enters the Sink will
// be consumed by our consumer.
val toConsumer: Sink[String, NotUsed] = runnableGraph.run()

// Feeding two independent sources into the hub.
AmqpSource.atMostOnceSource(
   NamedQueueSourceSettings(..)).runWith(toConsumer)
AmqpSource.atMostOnceSource(
   NamedQueueSourceSettings(..)).runWith(toConsumer)