繁体   English   中英

创建一个路由器角色,在akka java中参数化其路由的创建

[英]create a router actor parameterizing the creation of each one of its routees in akka java

我需要知道如何创建RoundRobinPool Router Actor,其中每个路由都有自己对另一个对象的引用。 每个路由所拥有的引用必须对于每个路由都不同。

一种方法是这样的:

ActorRef ref = system.actorOf(new RoundRobinPool(5).props(Props.create(Worker.class, new AnotherObject())), 
"router");

但是这种方法的麻烦之处在于,每个工作人员都具有对AnotherObject的相同引用,并且我不希望这种副作用。

可能是这样,但使用Router Actor:

List<Routee> routees = new ArrayList<Routee>();
    for (int i = 0; i < 5; i++) {
      ActorRef r = system.actorOf(Props.create(Worker.class, new AnotherObject()));
      routees.add(new ActorRefRoutee(r));
    }
    Router router = new Router(new RoundRobinRoutingLogic(), routees); // That is a Router I need an ActorRef

有人知道该怎么做吗?

干杯

查看有关路由器组的Akka Java文档。 您可以单独创建路由,然后通过config或以编程方式将其提供给路由器组。 在您的情况下,以编程方式可能就是您所追求的,因此您可以将不同的引用传递给每个路由。

您可以使用RoundRobinGroup 对于组,您可以创建所需的角色,然后在创建组时将角色的路径传递给组。 池和组之间的区别仅在于路由的创建方式。

用实际代码编辑

String parent = new String("/user/<whatever is your actual path to routees>");
List<String> routees = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
  ActorRef r = system.actorOf(Props.create(Worker.class, new AnotherObject()), “route” + i);
  routees.add(parent + “routee” + i);
}
system.actorOf(new RoundRobinGroup(routees).props(), “router”);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM