在Akka Java actor模型中,路由器可以使用非默认构造函数创建actor吗? [英] In Akka Java actor model, can a router create actors with non-default constructor?

查看:85
本文介绍了在Akka Java actor模型中,路由器可以使用非默认构造函数创建actor吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Akka Java actor模型中,如果我有RoundRobinRouter,则在调用其 tell()方法时,我希望路由器(作为主机)创建子actor使用非默认构造函数,因为我需要传递一些参数。我该怎么做?

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?

我知道我可以使用 Props 使用具有非默认构造函数的演员,但是

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?

谢谢!

推荐答案

构造路由器的道具是该路由器路由的道具,而不是路由器本身。

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(...))

所有路由都将是MyActor类型,并带有特定的构造函数。

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

您可以使用通常的Props做任何事情能够。有关更多信息,请参见 Akka文档

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

这篇关于在Akka Java actor模型中,路由器可以使用非默认构造函数创建actor吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆