当主管重新启动关联的 Actor 时,ActorRef 是否更新?
Is an ActorRef updated when the associated Actor is restarted by the supervisor?
如果我像这样创建一个日志 actor
val logger: ActorRef =
actorSystem.actorOf(Props(new Logger()))
并且记录器由于异常而重新启动,我的记录器停止写入磁盘。
我一直在发消息logger ! msg
我假设 ActorRef 在主管重新启动我的日志记录 actor 时没有更新是否正确?
ActorRef
应该由 Akka 正确更新以指向 actor 的新实例。 docs 明确指出:
A reference pointing to a terminated actor does not compare equal to
a reference pointing to another (re-created) actor with the same path.
Note that a restart of an actor caused by a failure still means that
it is the same actor incarnation, i.e. a restart is not visible for
the consumer of the ActorRef.
还有here:
When actorOf() is called it assigns an incarnation of the actor
described by the passed Props to the given path. An actor incarnation
is identified by the path and a UID. A restart only swaps the Actor
instance defined by the Props but the incarnation and hence the UID
remains the same.
The lifecycle of an incarnation ends when the actor is stopped. At
that point the appropriate lifecycle events are called and watching
actors are notified of the termination. After the incarnation is
stopped, the path can be reused again by creating an actor with
actorOf(). In this case the name of the new incarnation will be the
same as the previous one but the UIDs will differ. ...
An ActorRef always represents an incarnation (path and UID) not
just a given path. Therefore if an actor is stopped and a new one with
the same name is created an ActorRef of the old incarnation will not
point to the new one.
这是使用 ActorRef
的主要优势之一,否则与演员合作会更加不便。
您需要检查您的主管策略并确保 actor 确实已重新启动。
默认策略是:
final val defaultStrategy: SupervisorStrategy = {
def defaultDecider: Decider = {
case _: ActorInitializationException ⇒ Stop
case _: ActorKilledException ⇒ Stop
case _: Exception ⇒ Restart
}
OneForOneStrategy()(defaultDecider)
}
因此,如果你得到一个 Exception
你的 actor 将被重新启动并且 ActorRef
应该是有效的,但是如果你得到其他类型的 Throwable
然后它会被停止并且 ActorRef
将失效。
如果我像这样创建一个日志 actor
val logger: ActorRef =
actorSystem.actorOf(Props(new Logger()))
并且记录器由于异常而重新启动,我的记录器停止写入磁盘。
我一直在发消息logger ! msg
我假设 ActorRef 在主管重新启动我的日志记录 actor 时没有更新是否正确?
ActorRef
应该由 Akka 正确更新以指向 actor 的新实例。 docs 明确指出:
A reference pointing to a terminated actor does not compare equal to a reference pointing to another (re-created) actor with the same path. Note that a restart of an actor caused by a failure still means that it is the same actor incarnation, i.e. a restart is not visible for the consumer of the ActorRef.
还有here:
When actorOf() is called it assigns an incarnation of the actor described by the passed Props to the given path. An actor incarnation is identified by the path and a UID. A restart only swaps the Actor instance defined by the Props but the incarnation and hence the UID remains the same.
The lifecycle of an incarnation ends when the actor is stopped. At that point the appropriate lifecycle events are called and watching actors are notified of the termination. After the incarnation is stopped, the path can be reused again by creating an actor with actorOf(). In this case the name of the new incarnation will be the same as the previous one but the UIDs will differ. ...
An ActorRef always represents an incarnation (path and UID) not just a given path. Therefore if an actor is stopped and a new one with the same name is created an ActorRef of the old incarnation will not point to the new one.
这是使用 ActorRef
的主要优势之一,否则与演员合作会更加不便。
您需要检查您的主管策略并确保 actor 确实已重新启动。 默认策略是:
final val defaultStrategy: SupervisorStrategy = {
def defaultDecider: Decider = {
case _: ActorInitializationException ⇒ Stop
case _: ActorKilledException ⇒ Stop
case _: Exception ⇒ Restart
}
OneForOneStrategy()(defaultDecider)
}
因此,如果你得到一个 Exception
你的 actor 将被重新启动并且 ActorRef
应该是有效的,但是如果你得到其他类型的 Throwable
然后它会被停止并且 ActorRef
将失效。