简体   繁体   English

Akka - 重新发送“破坏”消息

[英]Akka - resending the “breaking” message

In the official akka 2.0.4 docs it says: 在官方的akka 2.0.4文档中,它说:

An actor restart replaces only the actual actor object; actor重新启动只替换实际的actor对象; the contents of the mailbox is unaffected by the restart, so processing of messages will resume after the postRestart hook returns. 邮箱的内容不受重新启动的影响,因此在postRestart挂钩返回后将继续处理消息。 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. 在重新启动时发送给actor的任何消息都将像往常一样排队到其邮箱。

Let's say I have a message that caused my actor to restart. 假设我有一条消息导致我的演员重新开始。 It isn't in the mailbox anymore, so it won't be processed by the actor that will take it's place. 它不再在邮箱中,因此它不会由将要占据它的角色处理。 If I want this message to be processed by the actor anyways (assuming the order doesn't matter in that case), would it be a bad idea for the actor to send the message to themself on restart? 如果我希望这个消息由演员处理(假设在这种情况下顺序无关紧要),演员在重启时将消息发送给自己是不是一个坏主意?

Some (pseudo)code to show what I mean: 一些(伪)代码显示我的意思:

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 => ;
    }
  }
}

This way the message that wasn't processed by the actor before it got restarted is pushed to the end of the queue for the new actor. 这样,在重新启动之前未由actor处理的消息被推送到新actor的队列末尾。

So my question is: Is there any reason I shouldn't be doing this? 所以我的问题是:我有什么理由不这样做吗?

The only thing I can think of is that if the message is for some reason malformed, it will never leave the system and cause the actor to be restarted regularly... 我唯一能想到的是,如果消息由于某种原因而导致格式错误,它将永远不会离开系统并导致演员定期重启...

You already get the failing message in preRestart (see: message: Option[Any]) so no need to stash it away yourself. 您已经在preRestart中收到了失败的消息(请参阅:message:Option [Any]),因此无需自己将其隐藏起来。 And yes, it is perfectly fine to re-send it to yourself, but beware of infinite restarts in combination with this since you'll most likely end up with a message that will never be discarded. 是的,将它重新发送给自己是完全没问题的,但要注意与此结合使用的无限重启,因为你最有可能最终得到一条永不丢弃的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM