gpt4 book ai didi

java - 如何在 Akka 2.5 中使用 Router 创建 actor Prop

转载 作者:行者123 更新时间:2023-12-02 13:00:39 26 4
gpt4 key购买 nike

在 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 和其他一些旧的 Router 不再存在了。将这些路由行为添加到参与者的正确方法是什么?

最佳答案

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

关于java - 如何在 Akka 2.5 中使用 Router 创建 actor Prop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44312797/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com