Is passing around ActorRef to other Actors good or bad ?

别说谁变了你拦得住时间么 提交于 2019-12-03 01:15:05
cmbaxter

There are a couple of good ways to "introduce" actor refs to actor instances that need them.

1) Create the actor with the refs it needs as constructor args (which is what you are doing)

2) Pass in the needed refs with a message after the instance is created

Your solution is perfectly acceptable, and even suggested by Roland Kuhn, the Tech Lead for Akka, in this post:

Akka actor lookup or dependency injection

It is perfectly valid, as stated in the Akka API documentation:

ActorRefs can be freely shared among actors by message passing.

In your case, you are passing them to the constructors, which is absolutely fine and the way it is supposed to be.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!