0

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.

Eugene Loy
  • 12,224
  • 8
  • 53
  • 79
StefanE
  • 817
  • 6
  • 20

2 Answers2

1

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.

pushy
  • 9,535
  • 5
  • 26
  • 45
  • This is actually a good place to look. Now I actually have the sender. However as a side requirement, would it be possible for me to also have the message which the actor was processing, when the error happened? – StefanE Sep 03 '13 at 21:18
  • The message should be accessible from the ActorContext, currentMessage IIRC – pushy Sep 04 '13 at 06:16
  • I'm using Akka 2.2.1 I can't seem to find the method currentMessage(), please refer to API. Thanks for your help btw. – StefanE Sep 04 '13 at 06:50
  • Sorry, I just tried to look it up. What I meant was a private field in an internal object (ActorCell), you have no access to it. I saw it during debugging and thought there would be a way to access it. The information is there, but I cannot find a way to retrieve it. – pushy Sep 04 '13 at 07:44
  • Hmm I actually thought that I could have the right sender. But it seems that the context I get with the OneForOneStrategy is not similar to when it failed. When I asked for sender() on the context I get the failed child actor, instead of who originally sent the message to the actor which failed. – StefanE Sep 04 '13 at 17:21
0

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

Heiko Seeberger
  • 3,702
  • 21
  • 20
  • I thought about that, but the main thing I would like to avoid, is that the developer should always remember do to this. If I could have it in the supervisor, I would be sure that I never forget when adding new children to the actor A in this case. Also all error handling would be in one place. – StefanE Sep 01 '13 at 15:56
  • Also for the other point, I'm not creating the exceptions myself. – StefanE Sep 01 '13 at 15:57