阿卡演员道具厂 [英] Akka Actor Props factory
问题描述
我和阿卡(Akka)互相认识。
Akka and I are getting to know each other.
这些是一个名为DemoActor的示例演员:
These is an example actor called DemoActor :
class DemoActor(magicNumber: Int) extends Actor {
def receive = {
case x: Int => sender() ! (x + magicNumber)
}
}
推荐做法文档的内容如下:在每个Actor的伴随对象上提供工厂方法是一个好主意,这有助于使合适的Props的创建尽可能地接近Actor的定义。 他们这样做:
In Recommended Practices section of the doc it states : "It is a good idea to provide factory methods on the companion object of each Actor which help keeping the creation of suitable Props as close to the actor definition as possible." Which they do like this :
object DemoActor {
def props(magicNumber: Int): Props = Props(new DemoActor(magicNumber))
}
问题 :为props方法指定工厂的区别是什么:
object DemoActor {
def props(magicNumber: Int): Props = Props(classOf[DemoActor], magicNumber)
}
如果您错过了它,区别是Props构造函数的参数:
In case you missed it, the difference was the argument to the Props constructor :
new DemoActor(magicNumber)
VS
classOf[DemoActor], magicNumber
在同一akka文档页面的道具部分中,它还提到在使用 Props(classOf [ActorWithArgs], arg1
)时:
在构造过程中验证了匹配构造函数的存在如果没有找到或匹配多个匹配的构造函数,则会导致IllegalArgumentEception。
From the same akka documentation page a bit further up in the Props section, it also mentions when using Props(classOf[ActorWithArgs], "arg1"
):
"The presence of a matching constructor is verified during construction of the Props object, resulting in an IllegalArgumentEception if no or multiple matching constructors are found."
那很好,不是吗?!?....
That's good, isn't it?!?....
推荐答案
那很好,不是吗?!?....
That's good, isn't it?!?....
是的,但是如果可以在编译时捕获错误,那就更好了。直接调用构造函数的好处是,编译器将捕获不匹配的构造函数的问题,而不是在运行时引发异常。
Yes, but it is even better if the error can be caught during compile time. The advantage of invoking the constructor directly is that the compiler will catch the problem of no matching constructor instead of an exception being thrown at runtime.
关于<$的有趣的事情c $ c>道具 应用
方法是,当您编写以下内容时:
An interesting thing about the Props
apply
method is that when you write:
Props(new DemoActor(magicNumber))
演员的构造函数是在创建Props实例时不会立即调用。构造函数调用是通过名称 而不是通过值传递的。您可以在道具
应用
方法的签名中看到以下内容:
the constructor of the actor is not invoked immediately when the Props instance is created. The constructor invocation is passed by name rather than by value. You can see this in signature of the Props
apply
method:
def apply[T <: Actor](creator: ⇒ T)(implicit arg0: ClassTag[T]): Props
请注意创建者参数中的向右箭头。这样就可以推迟创建者的构建,并可能在远程参与者的另一个过程中执行该构建器。
Notice the right arrow in the creator parameter. This allows the creator construction to be postponed, and potentially executed in another process for remote actors.
如果 new ,则可能存在危害。 code>操作将关闭范围并捕获一个不打算序列化或不可序列化的值,从而使Props对象也无法序列化。这就是为什么文档建议在actor的伴随对象中执行此操作的原因,以最大程度地减少关闭不打算序列化的数据的风险。
A potential hazard here is if the new
operation closes over the scope and captures a value that is not intended to be serialized or is not serializable, thus making the Props object also not serializable. This is why the documentation recommends doing this in the companion object of the actor—to minimize the risk of closing over data that is not intended to be serialized.
这篇关于阿卡演员道具厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!