Basic Custom Akka Supervisor in Java

I am trying to implement tasks using replay semantics using Akka . If an employee fails (throws an exception) during his job, in addition to rebooting it, I want to resend the job he was working on.

The approach that I am trying to use is the usual supervisor, but I can not get it to restart the worker on error. for example, Run the following code with Akka 1.1.3 and you will never see a reboot message:

  import akka.actor.ActorRef;
  import akka.actor.UntypedActor;
  import akka.actor.UntypedActorFactory;
  import akka.config.Supervision;

  import static akka.actor.Actors.actorOf;
  import static java.lang.System.out;

  public class Supervisor extends UntypedActor {
      private ActorRef worker;

      public static class Worker extends UntypedActor {
          @Override
          public void onReceive(Object message) {
              throw new RuntimeException("croak");
          }

          public void preRestart(Object reason) {
              out.println("supervisor is restarting me!");
          }

          public void postRestart(Object reason) {
              out.println("supervisor restarted me.");
          }
      }

      public static void main(String[] args) {
          ActorRef supervisor = actorOf(new UntypedActorFactory() {
              public UntypedActor create() {
                  return new Supervisor();
              }
          });

          supervisor.start();
          supervisor.sendOneWay("job");
      }

      @Override
      public void preStart() {
          getContext().setFaultHandler(new Supervision.OneForOneStrategy(
              new Class[]{RuntimeException.class},
              3,
              1000
          ));

          // why doesn't the compiler like this line?
          // worker = actorOf(Worker.class);

          worker = actorOf(new UntypedActorFactory() {
              public UntypedActor create() {
                  return new Worker();
              }
          });

          getContext().startLink(worker);
      }

      @Override
      public void onReceive(Object message) {
          worker.sendOneWay(message);
      }
  }

Any idea what I'm doing wrong?

Thank!

+5
source share
1 answer

These are the correct signatures for restart methods in Workeractor:

    @Override
    public void preRestart(Throwable reason) {
        out.println("supervisor is restarting me!");
    }

    @Override
    public void postRestart(Throwable reason) {
        out.println("supervisor restarted me.");
    }

.

+6

All Articles