首页 > 解决方案 > Constructor vs. preStart: when to use which?

问题描述

In Akka you can initialize the actor's state within the constructor. Furthermore, as part of the actor's lifecycle, the preStart method can be overridden to initialize the actor's state and will be called:

The two ways (constructor and preStart) seem to cover overlapping phases of the actor's startup phase, with the slight difference that the constructor is guaranteed to be called upon restart, whereas preStart can be skipped, provided that you override postRestart.

Are there recognized or documented best practices or patterns regarding which one to use in which case?

标签: akka

解决方案


通过构造函数初始化的文档中:

使用构造函数进行初始化有很多好处。首先,它可以使用val字段来存储在 Actor 实例生命周期内不会改变的任何状态,从而使 Actor 的实现更加健壮。构造函数在创建actor实例时调用actorOf并且在重新启动时调用,因此actor的内部总是可以假设发生了正确的初始化。

通过 preStart 初始化的文档中:

Actor的方法preStart()仅在第一个实例的初始化期间直接调用一次,即在其创建时ActorRef。在重新启动的情况下,preStart()从 调用postRestart(),因此如果没有被覆盖,preStart()则在每次重新启动时调用。但是,通过覆盖postRestart()可以禁用此行为,并确保只有一次调用preStart().

此模式的一个有用用法是ActorRefs在重新启动期间禁用为子项创建新的。这可以通过覆盖来实现preRestart()

如果您希望在每次实例化 Actor 时都进行初始化(包括重新启动:在重新启动时,会创建底层 Actor 的新实例),请通过构造函数使用初始化。示例场景是每当创建一个actor时使用不变状态。

如果您希望仅在创建 Actor 的第一个实例时进行初始化,请使用初始化 viapreStart和 overridepostRestart以不调用preStart。示例用例是在重新启动时保留子角色(默认情况下,preRestart停止角色的子角色,这就是文档提到preRestart此示例用例的覆盖的原因)。


推荐阅读