简体   繁体   English

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

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

I need to know how to create a RoundRobinPool Router Actor in which each one of its routees has its own reference to another object . 我需要知道如何创建RoundRobinPool Router Actor,其中每个路由都有自己对另一个对象的引用。 That reference that is held by every routee must be different for each routee. 每个路由所拥有的引用必须对于每个路由都不同。

A way to do that would be something like this: 一种方法是这样的:

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

but the trouble with this approach is that every worker has the same reference to AnotherObject and I do not wish that side effect. 但是这种方法的麻烦之处在于,每个工作人员都具有对AnotherObject的相同引用,并且我不希望这种副作用。

It would be something like this, but with a Router Actor: 可能是这样,但使用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

Has someone any idea how to do that? 有人知道该怎么做吗?

Cheers 干杯

Check out the Akka Java documentation regarding Router groups . 查看有关路由器组的Akka Java文档。 You can create your routees individually and then provide them to your router group either via config or programmatically. 您可以单独创建路由,然后通过config或以编程方式将其提供给路由器组。 In your case programmatically is probably what you are after so you can pass a different reference to each routee. 在您的情况下,以编程方式可能就是您所追求的,因此您可以将不同的引用传递给每个路由。

You can use a RoundRobinGroup . 您可以使用RoundRobinGroup With a group, you create the actors you want and then pass the paths of the actors to the Group when creating the group. 对于组,您可以创建所需的角色,然后在创建组时将角色的路径传递给组。 The difference between a pool and a group is only how the routees are created. 池和组之间的区别仅在于路由的创建方式。

Edited with the actual code 用实际代码编辑

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