6
votes

I have parent actor (A) with two child actors (B). I made a supervisor strategy in A, so in case a specific exception happens in B, B will be restarted.

How can I resend the message which caused the exception in B to B again?

1
Does somebody have an advice for me?Simon

1 Answers

11
votes

What I've done in B is to send the message again to B in preRestart, see code below.

  @Override
  public void preRestart(final Throwable reason, final scala.Option<Object> message) throws Exception
  {
    getSelf().tell(message.get(), getSender());
  };

To ensure I don't end in an endless loop, I configure the supervisor strategy in A as follows:

  private final SupervisorStrategy strategy = new OneForOneStrategy(3, Duration.Inf(),
      new Function<Throwable, SupervisorStrategy.Directive>()
  {   
    @Override
    public Directive apply(final Throwable t) throws Exception
    {
      if (t instanceof SpecificException)
      {
        return SupervisorStrategy.restart();
      }
      return SupervisorStrategy.escalate();
    }
  });    

This should gurarantee, that the problematic message is resent only three times. Could somebody give me an advice if this is good practice or link me to a better solution?