Akka inform remote actor on error

494 views Asked by At

I have a design challenge in regards to a new akka application that I'm building. The issue/challenge is: On the client side I have made a simple actor which sends a request, and then using become() in order to wait for a proper server answer, of course also including a timeout message, in case I don't get an answer in proper time. The interesting thing is however on the server side. Here i have the following construction:

Actor A (Configured as a round robin router) this router is receiving all request from the client.

Actor A then forwards message to Actor A1, A2...Ax which all have been created in the context of actor A meaning it's the supervisor of them.

In the normal case the Actor Ax would be able to just reply to the sender, since the message is forwarded, however... In case of an error I would like to besides log it on the server log, also to give the user som kind of information on the error that has happened.

In the perfect world I would prefer to be able to somehow in the supervisor strategy to just say something like getErrorActorsLastSender() and then get the ActorRef of the client which caused the issue. The reason why I prefer this, is that then I would have one place to have all the error handling, and all unforseen exceptions would always be handled at least in some generic way.

The alternative is to override the prerestart() method on each child actor, and then make the supervisor strategy to restart the actor when an exception is thrown. However this would require me to implement this method for x child actors.

Any good suggestions, if this is possible from supervisor strategy?

Thanks in advance.

2

There are 2 answers

2
Heiko Seeberger On

One way to achieve your goals is to encapsulate the sender in the Exception. If you throw the Exception yourself, this should be straightforward.

5
pushy On

Have you tried creating your own supervisor strategy, for example by extending OneForOneStrategy? There is a method called handleFailure which takes (among others) the child and the cause of the failure. Also you will get the ActorContext, which gives you the sender of the message that caused the error, I think you should be able to do what you want when you override this method.