Is forwarding a message to all children via an act

2019-08-31 04:17发布

I have a scenario where when an actor receives a specific message it must forward the message to all of it's children that exist at that time.

I think I have 2 options to solve this.

getContext().actorSelection("*").forward(message, getContext());

or

getContext().getChildren().forEach(child -> child.forward(message, getContext()));

Without fully understanding the internal implementation of actor selection it's hard to know which will perform better. I plan to perform some bench marks with the kind of scale I am expecting to require but would appreciate any insight to this that experienced users may have.

Thanks

标签: java akka
1条回答
The star\"
2楼-- · 2019-08-31 04:55

I don't benchmark it, but I was looking the implementation of both options. Both should be practically very similar in terms of performance, because they use an iterable of actor references ultimately.

On the other hand, you can check that the string "*" is translated into a expression and a forward in an actorSelection will just match that expression with the children of the actor, and then forward the message to each of the matching children.

matchingChildren.foreach(_.tell(sel.msg, sender))

and do that recursively if needed (for example in "*/*").

So I would recommend to use getChildren to avoid the overload of parse the string ("*"), translate it in a regular expression, and filter the matching children.

查看更多
登录 后发表回答