もう1つの戦略は、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");
したがって、ルーターは単に「配信」メッセージを送信し、ディスパッチャーは選択されたアクターを実行します(そして、ワークスティーリングも実装します)