加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 当主管重新启动关联的Actor时,是否更新了ActorRef?

发布时间:2020-12-16 19:19:28 所属栏目:安全 来源:网络整理
导读:如果我像这样创建一个日志记录演员 val logger: ActorRef = actorSystem.actorOf(Props(new Logger())) 并且记录器由于异常而重新启动,我的记录器停止写入磁盘. 我一直在发送消息记录器!味精 假设当主管重新启动我的日志记录演员时,ActorRef没有更新,我是否
如果我像这样创建一个日志记录演员

val logger: ActorRef =
    actorSystem.actorOf(Props(new Logger()))

并且记录器由于异常而重新启动,我的记录器停止写入磁盘.

我一直在发送消息记录器!味精

假设当主管重新启动我的日志记录演员时,ActorRef没有更新,我是否正确?

解决方法

应该由Akka正确更新ActorRef以指向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.

这是使用ActorRefs的主要优点之一,否则与actor合作会更加不方便.

您需要检查您的主管策略并确保实际重新启动了actor.
默认策略是:

final val defaultStrategy: SupervisorStrategy = {
  def defaultDecider: Decider = {
    case _: ActorInitializationException ? Stop
    case _: ActorKilledException         ? Stop
    case _: Exception                    ? Restart
  }
  OneForOneStrategy()(defaultDecider)
}

因此,如果你得到一个Exception,你的actor将被重启并且ActorRef应该是有效的,但是如果你得到其他类型的Throwable,那么它将被停止并且ActorRef将变为无效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读