Package org.akka.essentials.supervisor.example1

Source Code of org.akka.essentials.supervisor.example1.SupervisorActor

package org.akka.essentials.supervisor.example1;

import static akka.actor.SupervisorStrategy.escalate;
import static akka.actor.SupervisorStrategy.restart;
import static akka.actor.SupervisorStrategy.resume;
import static akka.actor.SupervisorStrategy.stop;

import org.akka.essentials.supervisor.example1.MyActorSystem.Result;

import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.actor.OneForOneStrategy;
import akka.actor.Props;
import akka.actor.SupervisorStrategy;
import akka.actor.SupervisorStrategy.Directive;
import akka.actor.UntypedActor;
import akka.japi.Function;


public class SupervisorActor extends UntypedActor {

  public ActorRef childActor;

  public SupervisorActor() {
    childActor = getContext().actorOf(new Props(WorkerActor.class),
        "workerActor");
  }

  private static SupervisorStrategy strategy = new OneForOneStrategy(10,
      Duration.create("10 second"), new Function<Throwable, Directive>() {
        public Directive apply(Throwable t) {
          if (t instanceof ArithmeticException) {
            return resume();
          } else if (t instanceof NullPointerException) {
            return restart();
          } else if (t instanceof IllegalArgumentException) {
            return stop();
          } else {
            return escalate();
          }
        }
      });

  @Override
  public SupervisorStrategy supervisorStrategy() {
    return strategy;
  }

  public void onReceive(Object o) throws Exception {
    if (o instanceof Result) {
      childActor.tell(o, getSender());
    } else
      childActor.tell(o);
  }
}
TOP

Related Classes of org.akka.essentials.supervisor.example1.SupervisorActor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.