Question

I am using Akka (Java) in my project for providing a retry scheme. So I have a supervisor actor, which on receiving a message delegates it to a supervised actor. I have a one-for-one strategy on the supervisor to restart (unconditionally).

The supervised actor has a preRestart hook, which sends the message to self.

@Override
    public void preRestart(Throwable reason, Option<Object> message){
        //send it to the supervisor so that it can be enqueued once again to retry
        if(reason.getCause() instanceof SQLException){
              log.error("Not retrying since there seems to be a problem with the SQL itself!");
              super.preRestart(reason, message);

        }
        else{
            log.warn(""+state+" Trying to redo a processing: "+((RecordData)message.get()).getRecord());

            getSelf().tell(message.get(), getSender());
            super.preRestart(reason, message);
        }
    }

Now I want to preserve the internal state of the failed actor as well! I understand the state will be preserved only if my strategy is 'resume', but in that case the onRestart hook won't be invoked and the message will be lost.

Question:

1. Is the only way to achieve this, is to set the the state in the message itself and restart?

2. If the state maintains a sequential order, then I need to provide an 'ordered' mailbox implementation (dequeue based). Is that a correct understanding?

Was it helpful?

Solution

I doubt if you can send a message to an Actor that is in preRestart phase.

Your strategy should be that the actor is reset upon crash/restart. If you want to keep the internal state, you have to pass it to somewhere else (other actor, database) to ask it again when the new actor is started.

Why not indeed implement some more of the logic in the supervisor?

I would post some example code here, if I knew what version of Akka are you using exactly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top