title: Wiring Akka PoisonPill to actor system and JVM shutdown hooks
body: <p>Akka/<strong>Java</strong> here, although I have a basic understanding of Scala. New to Akka. I have a <code>Master</code> class that starts up when the actor system fires up, which manages three children: <code>Fizz</code>, <code>Buzz</code> and <code>Foo</code>.</p>
  
  <p>When <code>Master</code> starts up, that call to <code>doSomething()</code> can throw a <code>NoSuchElementException</code>. If it does, I would like the <code>Master</code> to shut down its three children, kill itself, shut down the actor system as a whole, and then invoke a custom system shutdown hook. My best attempt thus far:</p>
  
  <pre><code>public class MyApp {
      public static void main(String[] args) {
          ActorRef master = actorSystem.actorOf(Props.create(Master.class));
          master.tell(new Init(), ActorRef.noSender()); 
  
          Runtime.getRuntime().addShutdownHook(new Thread() {
              @Override
              void run() {
                  System.out.println("Shutting down!");
              }
          });   
      }
  }
  
  public class Master extends AbstractActor {
      private Logger log = LoggerFactory.getLogger(this.getClass());
  
      private ActorRef fizz;
      private ActorRef buzz;
      private ActorRef foo;
  
      public Master() {
          super();
      }
  
      @Override
      public Receive createReceive() {
          return receiveBuilder()
              .match(Init.class, init -&gt; {
                  try {
                      fizz = context().actorOf(Props.create(Fizz.class));
                      buzz = context().actorOf(Props.create(Buzz.class));
                      foo = context().actorOf(Props.create(Foo.class));
  
                      long metric = doSomething();
  
                      log.info("After all the children started up \"metric\" was: {}", metric);                    
                  } catch(NoSuchElementException ex) {
                      self().tell(PoisonPill.getInstance(), self());
                  }
              }).build();
      }
  }
  </code></pre>
  
  <p>My thinking here is:</p>
  
  <ul>
  <li>Since <code>Master</code> is the top-most actor, I can't define a <code>SupervisorStrategy</code> to handle the thrown <code>NoSuchElementException</code> for me, so I have to put a try-catch in there to handle it</li>
  <li>My <em>understanding</em> of <code>PoisonPill</code> is that it shuts down the receiving actor's children and then shuts the actor down</li>
  <li>However I'm still fuzzy as to whether <code>PoisonPill</code> shuts the actor system down <em>if</em> the actor happens to be the root/top-level actor, and I'm also not seeing how I can wire the <code>PoisonPill</code> to not only shut the actor system down, but also engage the JVM's shutdown hook</li>
  </ul>
  
  <p>When I run this code I don't see any evidence of the actor system shutting down, it just hangs. Any ideas how I can wire all this together to achieve the desired affect?</p>