Akka-重新发送“中断"消息信息 [英] Akka - resending the "breaking" message

查看:95
本文介绍了Akka-重新发送“中断"消息信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在官方的 akka 2.0.4文档中,它说:

actor重新启动仅替换实际的actor对象;邮箱的内容不受重启影响, 因此消息处理将在postRestart挂钩返回后恢复.触发了 异常将不会再次收到.在重新启动时发送给actor的任何消息都将排队到它的队列中 邮箱照常.

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.

假设我有一条消息导致我的演员重启.它不再位于邮箱中,因此不会由代替它的actor处理.如果我仍然希望该消息由actor处理(假设在这种情况下顺序无关紧要),那么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 => ;
    }
  }
}

通过这种方式,actor在重新启动之前未处理的消息将被推送到新actor的队列末尾.

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.

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

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...

推荐答案

您已经在preRestart中收到了失败的消息(请参阅:消息:Option [Any]),因此无需自己将其藏起来.是的,将它重新发送给自己是完全可以的,但是请注意,与之结合使用的无限重启,因为您很可能最终收到一条永远不会被丢弃的消息.

You already get the failing message in preRestart (see: message: Option[Any]) so no need to stash it away yourself. 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.

这篇关于Akka-重新发送“中断"消息信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆