How to attach multiple actors as sources to an Akka stream?

旧巷老猫 提交于 2019-12-06 03:02:44

问题


I am trying to build and run an akka stream flow (in Java DSL) with 2 actors as sources, then a merge junction and then 1 sink:

    Source<Integer, ActorRef> src1 = Source.actorRef(100, OverflowStrategy.backpressure());
    Source<Integer, ActorRef> src2 = Source.actorRef(100, OverflowStrategy.backpressure());
    Sink<Integer, BoxedUnit> sink = Flow.of(Integer.class).to(Sink.foreach(System.out::println));

    RunnableFlow<BoxedUnit> closed = FlowGraph.factory().closed(sink, (b, out) -> {
        UniformFanInShape<Integer, Integer> merge = b.graph(Merge.<Integer>create(2));
        b.from(src1).via(merge).to(out);
        b.from(src2).to(merge);
    });

    closed.run(mat);

My question is how do I obtain ActorRef references to the source actors in order to send them messages? In case of 1 actor, I wouldn't be using graph builder, and then the .run() or runWith() method would return the ActorRef object. But what to do in case of many source actors? Is it even possible to materialize such a flow?


回答1:


Answering my own question in case someone needs it.

Using jrudolph's advice, I was able to use actors like this (in actual code I did something nicer than a list of 2 ActorRefs):

    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);


来源:https://stackoverflow.com/questions/30077766/how-to-attach-multiple-actors-as-sources-to-an-akka-stream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!