如何将多个演员作为源添加到Akka流中?

编程入门 行业动态 更新时间:2024-10-22 14:31:50
本文介绍了如何将多个演员作为源添加到Akka流中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用2个actor作为源,然后是一个合并结,然后是1个接收器来构建和运行akka流(在Java DSL中):

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

我的问题是如何获取对源actor的ActorRef引用以便向其发送消息?如果有1个actor,我将不会使用图形生成器,然后.run()或runWith()方法将返回ActorRef对象。但是,如果有许多来源参与者,该怎么办?

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?

推荐答案

在有人需要的情况下回答我自己的问题。

Answering my own question in case someone needs it.

使用jrudolph的建议,我可以使用这样的actor(在实际代码中,我做的比2个ActorRefs列表还要好):

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

更多推荐

如何将多个演员作为源添加到Akka流中?

本文发布于:2023-11-25 02:58:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何将   演员   Akka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!