AKKA: Painless Actor Error Notifications

十年热恋 提交于 2020-01-24 04:14:05

问题


Beautiful People Lifestyle

  • A component BEAUTIFUL is using her internal akka.actor.Actor to do certain things.. ( such as "acting up" for example )

  • There are other MAN components that would really like to "interact" with the BEAUTIFUL

  • When the BEAUTIFUL finds a MAN worthy, before "interacting" with him, she agrees to take in his phone number ( let's call it an ErrorHandler ), you know just to give him a call in case he left in the morning and forgot his Rolex on her bed side table

  • The BEAUTIFUL though is "high maintenance" ( aren't they all.. ), and every time something bad happens inside the BEAUTIFUL's internal Actor ( e.g. an OmgBrokenNailException, UglyPurseThrowable, etc.. ), she goes nuts, needs to stop completely and to call a MAN using that phone number ( e.g. errorHandler.getOnItNowHoney( message, throwable ) )

Beautiful People Problem

AKKA supervisors allow to register two types of FaultHandlers => OneForOneStrategy and AllForOneStrategy, where if the underlying Actor overrides preRestart/postRestart, it gets access to an actual "throwable" e.g.:

override def preRestart( reason: Throwable ) { // do with throwable... }

The problem is both of these strategies will try to restart the Actor(s), which is not something that I am looking for. I am looking for the Actor to call an external ErrorHandler with the "throwable" and stop.

If I don't use these strategies, when exception is thrown from within the Actor, a postStop is called on the Actor, which is cool, but it does not take in a "throwable":

override def postStop() { // no access to "throwable"... }

Help the MAN to get that throwable


回答1:


In Akka 2 you should use DeathWatch




回答2:


If I understand your problem correctly, I think it would make sense to wrap your message processing in a try/catch block and let the actor handle sending the Throwable and stopping itself.

def receive = {
  case message => try { message match {
    case ... //your actual message processing goes here
  }} catch {
    case t: Throwable => {
      errorHandler ! t
      self.stop
    }
  }
}


来源:https://stackoverflow.com/questions/8746521/akka-painless-actor-error-notifications

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!