如何在Akka 2.5中使用Router创建actor道具

时间:2017-06-01 16:29:26

标签: java akka

在Akka 2.0中我们曾经有过

List<String> routeePaths = actorRefs.stream()
                    .map(e -> e.path().toString())
                    .collect(toList());

Props routerProps = Props.empty().withRouter(new BroadcastRouter(routeePaths));

但在Akka 2.5中,BroadcastRouter和其他一些旧路由器已不复存在了。什么是将这些路由行为添加到演员的正确方法。

1 个答案:

答案 0 :(得分:1)

Akka 2.5有BroadcastRoutingLogic。以下示例来自documentation

Router router;
{
  List<Routee> routees = new ArrayList<Routee>();
  for (int i = 0; i < 5; i++) {
    ActorRef r = getContext().actorOf(Props.create(Worker.class));
    getContext().watch(r);
    routees.add(new ActorRefRoutee(r));
  }
  router = new Router(new BroadcastRoutingLogic(), routees);
}

您还可以使用BroadcastPoolBroadcastGroup

配置中定义

BroadcastPool

akka.actor.deployment {
  /parent/router13 {
    router = broadcast-pool
    nr-of-instances = 5
  }
}

ActorRef router13 = getContext().actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router13");
代码中定义的

BroadcastPool

ActorRef router14 = getContext().actorOf(new BroadcastPool(5).props(Props.create(Worker.class)), "router14");
配置中定义

BroadcastGroup

akka.actor.deployment {
  /parent/router15 {
    router = broadcast-group
    routees.paths = ["/user/workers/w1", "/user/workers/w2", "/use/workers/w3"]
  }
}

ActorRef router15 = getContext().actorOf(FromConfig.getInstance().props(), "router15");
代码中定义的

BroadcastGroup

List<String> paths = Arrays.asList("/user/workers/w1", "/user/workers/w2", "/user/workers/w3");
ActorRef router16 = getContext().actorOf(new BroadcastGroup(paths).props(), "router16");