How to Implement Retry on Transient Actor Failure?
I'm in a typical situation where I have a group of worker actors that are being fed jobs from a queue. Worker failures can be either transient and due to the environment, or permanent and due to an invalid job.
Inside the worker actor I'm handling the permanent failure case, but how can I implement a retry for the transient cases? With a supervisor I can have the actor restarted, which is desirable to try and reset to a working state, but how then can I retry the message it was working on when it failed?
开发者_运维百科Thanks!
You need to implement the Command pattern. A Command is an object that encapsulates an operation to be performed, along with its arguments, and exists independent of the Actor. If the Actor fails in a fashion that is retryable, the supervisor resubmits the Command to the Actor. If the Actor is fed a stream and processes messages, then the Actor must populate a list of Commands and record completion so it can restart at the right place. If the stream isn't rewindable, then you have to queue up ALL the commands before starting, and the Actor must record completion status on each Command, so the chain can be resubmitted on transient failure.
The Command pattern can also be used to implement history and undo.
If you are working with Akka 1.2 or master, you can override preRestart to obtain the message that was current when the actor crashed.
精彩评论