1
votes

I'm reading Akka documentation and now I'm at the section about UntypedActors. I decided to try some examples:

Here are my actors:

Parent

private static class MyUntypedActor extends UntypedActor{

    public void onReceive(Object message) throws Exception {
        System.out.println("Recieved: " + message);
    }

    @Override
    public void preStart(){
        getContext().actorOf(AnotherUntypedActor.props()).tell("Process it", getSelf());
    }

    public static Props props(){
        return Props.create(MyUntypedActor.class);
    }
}

Child

private static class AnotherUntypedActor extends UntypedActor{

    public static Props props(){
        return Props.create(AnotherUntypedActor.class);
    }

    public void onReceive(Object message) throws Exception {
        System.out.println("My: " + message);
        throw new RuntimeException("Crashed: " + getSelf());
    }
}

main:

public static void main(String[] args) throws TimeoutException {
    ActorSystem system = ActorSystem.create();
    Inbox inbox = Inbox.create(system);
    ActorRef actorRef = system.actorOf(MyUntypedActor.props());
    inbox.send(actorRef, "Message");
}

so, my child actors experienced failure and I thought the it should have notified the parent somehow.

But what I recieved was this:

Recieved: Message
My: Process it 
[ERROR] [07/14/2016 19:05:13.726] [default-akka.actor.default-dispatcher-4] [akka://default/user/$a/$a] Crashed: Actor[akka://default/user/$a/$a#-950392568]

What does the supervision do actually? Child actor had faulted and what? I got just an error message in a log. What does supervisorStrategy mean? It's by default set to

OneForOneStrategy(-1,Duration.Inf,true)
1

1 Answers

1
votes

By using supervisor strategy, you decide what should be done with supervised actor if it fails. You have to override supervisionStrategy() method within your parent actor and define strategy. I.e. (not sure if it's correct since I use Scala for Akka)

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

    private static SupervisorStrategy strategy =
            new OneForOneStrategy(10, Duration.create("1 minute"),
                    t -> {
                        if (t instanceof SomeException) {
                            return restart();
                        } else {
                            return stop();
                        }
                    });

In this case, if SomeException occurs, actor will be restarted. Otherwise, it will be stopped. You can choose one of four strategies. Read documentation

Tip: Create specific exceptions!