另一种策略可以是使用 BalancingDispatcher 和 RoundRobinRouter(作为演员“池”)。来自 Akka 文档:
BalancingDispatcher
# This is an executor based event driven dispatcher that will try to redistribute work from busy actors to idle actors.
# All the actors share a single Mailbox that they get their messages from.
It is assumed that all actors using the same instance of this dispatcher can process all messages that have been sent to one of the actors; i.e. the actors belong to a pool of actors, and to the client there is no guarantee about which actor instance actually processes a given message.
# Sharability: Actors of the same type only
# Mailboxes: Any, creates one for all Actors
# Use cases: Work-sharing
在 application.conf 中定义您的调度程序或在启动时以编程方式加载它。
private final static Config akkaConfig = ConfigFactory.parseString(
"my-dispatcher.type = BalancingDispatcher \n" +
"my-dispatcher.executor = fork-join-executor \n" +
"my-dispatcher.fork-join-executor.parallelism-min = 8 \n" +
"my-dispatcher.fork-join-executor.parallelism-factor = 3.0 \n" +
"my-dispatcher.fork-join-executor.parallelism-max = 64 "
);
然后为路由定义路由器和调度程序。
getContext().actorOf(new Props(MyActor.class).withRouter(new RoundRobinRouter(10)).withDispatcher("my-dispatcher"), "myActor");
所以路由器将简单地继续“分发”消息,调度程序将运行一个选定的参与者(它也实现了工作窃取)