可以为每个 Actor 指定自定义路由或自定义构造函数参数
Possible to specify custom Routees or custom constructor arguments for each Actor
我想这可能是不可能的,因为监督策略需要重新创建演员,但是我可以将自定义演员提供给路由器吗?或者至少:自定义构造函数参数?
如果不是,为什么 akka 不允许这样做?路由不应该以这种方式使用吗?顺便说一句,我正在使用 BalancingPool。
这在 Scala 中似乎是可行的 How to create routers in akka with parameterized actors? 但我无法在 java 中解决这个问题。
public class Master extends UntypedActor {
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 RoundRobinRoutingLogic(), routees);
}
// ...
}
因此您可以按照自己的方式创建路由。
池不可能,因为池从相同的 Props
定义创建多个实例,但通常路由是可能的。来自文档:
This type of router actor comes in two distinct flavors:
- Pool - The router creates routees as child actors and removes them from the router if they terminate.
- Group - The routee actors are created externally to the router and the router sends messages to the specified path using actor selection,
without watching for termination.
如果您创建一个组而不是一个池,那么该组可以包含您想要的任何演员。
我想这可能是不可能的,因为监督策略需要重新创建演员,但是我可以将自定义演员提供给路由器吗?或者至少:自定义构造函数参数?
如果不是,为什么 akka 不允许这样做?路由不应该以这种方式使用吗?顺便说一句,我正在使用 BalancingPool。
这在 Scala 中似乎是可行的 How to create routers in akka with parameterized actors? 但我无法在 java 中解决这个问题。
public class Master extends UntypedActor {
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 RoundRobinRoutingLogic(), routees);
}
// ...
}
因此您可以按照自己的方式创建路由。
池不可能,因为池从相同的 Props
定义创建多个实例,但通常路由是可能的。来自文档:
This type of router actor comes in two distinct flavors:
- Pool - The router creates routees as child actors and removes them from the router if they terminate.
- Group - The routee actors are created externally to the router and the router sends messages to the specified path using actor selection, without watching for termination.
如果您创建一个组而不是一个池,那么该组可以包含您想要的任何演员。