オプション1
私はthis questionに記載されているようにDistributedPubSubMediator
を使用して、それを解決しhereを文書。
private ActorRef mediator = DistributedPubSub.get(getContext().system()).mediator();
@Override
public void onReceive(Object msg) throws Exception {
String msgStr = msg.toString();
String val = msgStr.substring(4);
if (msgStr.startsWith("add")) {
ActorRef act = context().actorOf(Props.create(User.class, val), val);
// subscribe the newly created user on topic "allUsers"
mediator.tell(new DistributedPubSubMediator.Subscribe("allUsers", act), self());
System.out.println("user created: " + act);
} else if (msgStr.startsWith("say")) {
// broadcast text message to all subscribed users
mediator.tell(new DistributedPubSubMediator.Publish("allUsers", new Text(val)), self());
}
}
オプション2
二成功したオプションは、BroadcastGroupルータを使用することです。重要なことは、クラスタリングが構成で有効になっていることです。
akka.actor.deployment {
/allUsers {
router = broadcast-group
routees.paths = ["/user/service/*"]
cluster {
enabled = on
allow-local-routees = on
}
}
}
その後、文書化されているとおりに簡単に使用できます。
ActorRef allUsers = system.actorOf(FromConfig.getInstance().props(), "allUsers");
[...]
allUsers.tell(new Text(val), self());