In Akka Java actor model, can a router create actors with non-default constructor?

不羁岁月 提交于 2019-12-12 18:16:10

问题


In Akka Java actor model, if I have a RoundRobinRouter, when its tell() method is called, I want the router (as the master) to create children actors with non-default constructor because I need to pass in some parameters. How can I do this?

I understand that I can an actor with non-default constructor using Props, but how is it used when the master actor is a router?

Thanks!


回答1:


The props in the construction of a Router is the props for the routees of that router, not the router itself.

You could simply do something like:

system.actorOf(new Props(new UntypedActorFactory() {
    public UntypedActor create() {
      return new MyActor("foo", "bar");
    }
  }).withRouter(...))

And all the routees will be of type MyActor with the specific constructor called.

You can do anything with the Props that you normally can. For more information see The Akka Docs



来源:https://stackoverflow.com/questions/12889490/in-akka-java-actor-model-can-a-router-create-actors-with-non-default-constructo

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