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

scala – Akka – 重新发出“打破”消息

发布时间:2020-12-16 09:34:25 所属栏目:安全 来源:网络整理
导读:在官方 akka 2.0.4 docs说: An actor restart replaces only the actual actor object; the contents of the mailbox is unaffected by the restart, so processing of messages will resume after the postRestart hook returns. The message that trigger
在官方 akka 2.0.4 docs说:

An actor restart replaces only the actual actor object; the contents of the mailbox is unaffected by the restart,
so processing of messages will resume after the postRestart hook returns. The message that triggered the
exception will not be received again. Any message sent to an actor while it is being restarted will be queued to its
mailbox as usual.

假设我有一个消息,导致我的演员重新启动。它不再在邮箱中,所以它不会被演员处理。如果我想让这个消息由演员进行处理(假设在这种情况下,这个顺序并不重要),那么演员在重新启动时将消息发送到自己是一个坏主意?

一些(伪)代码来显示我的意思:

class ResendingActor extends Actor {
  var curMessage: Option[MyMessage] = None
  def receive = {
    case MyMessage(x) => {
      curMessage = Some(MyMessage(x))
      /* processing */
      curMessage = None
    }
  }
  override def preRestart(reason: Throwable,message: Option[Any]) {
    curMessage match {
      case Some(x) => self ! x
      case None => ;
    }
  }
}

这样,在重新启动之前未被演员处理的消息被推送到新演员队列的末尾。

所以我的问题是:有什么理由我不应该这么做吗?

我唯一可以想到的是,如果信息由于某种原因导致错误,它将永远不会离开系统,并导致演员定期重新启动…

解决方法

您已经在preRestart中获得了失败的消息(请参阅:message:Option [Any]),因此无需将其隐藏。是的,将它重新发送给自己是完全正确的,但要注意无限次重新启动,结合这一点,因为你最有可能会得到永远不会被丢弃的消息。

(编辑:李大同)

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

    推荐文章
      热点阅读