问题
Beautiful People Lifestyle
A component
BEAUTIFUL
is using her internalakka.actor.Actor
to do certain things.. ( such as "acting up" for example )There are other
MAN
components that would really like to "interact" with theBEAUTIFUL
When the
BEAUTIFUL
finds aMAN
worthy, before "interacting" with him, she agrees to take in his phone number ( let's call it anErrorHandler
), you know just to give him a call in case he left in the morning and forgot his Rolex on her bed side tableThe
BEAUTIFUL
though is "high maintenance" ( aren't they all.. ), and every time something bad happens inside theBEAUTIFUL
's internal Actor ( e.g. anOmgBrokenNailException
,UglyPurseThrowable
, etc.. ), she goes nuts, needs to stop completely and to call aMAN
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