I know a similar question has already been asked:
How to attach multiple actors as sources to an Akka stream?
But I would like to do the same thing, however I don't know the number of sources in advance. So how can I dynamically add multiple sources to an akka stream?
For reference, this is the accepted answer from the other question, handling sources s1 and s2:
Source<Integer, ActorRef> src1 = Source.actorRef(100, OverflowStrategy.fail());
Source<Integer, ActorRef> src2 = Source.actorRef(100, OverflowStrategy.fail());
Sink<Integer, BoxedUnit> sink = Flow.of(Integer.class).to(Sink.foreach(System.out::println));
RunnableFlow<List<ActorRef>> closed = FlowGraph.factory().closed(src1, src2, (a1, a2) -> Arrays.asList(a1, a2), (b, s1, s2) -> {
UniformFanInShape<Integer, Integer> merge = b.graph(Merge.<Integer>create(2));
b.from(s1).via(merge).to(sink);
b.from(s2).to(merge);
});
List<ActorRef> stream = closed.run(mat);
ActorRef a1 = stream.get(0);
ActorRef a2 = stream.get(1);
However in my case, I'm adding sources as new sources come online, and deleting them as they go away.
Thanks!