简体   繁体   中英

Proper shutdown of Akka actor system in web application

I'm using Akka in one of my web applications. The application is a standard Spring app that gets initialized in the Tomcat servlet container.

I have a simple ServletContextListener like this:

public class AkkaInitializer implements ServletContextListener {

      @Override
      public void contextInitialized(ServletContextEvent servletContextEvent) {
           ActorSystem system = ActorSystem.create("system");
           // initialize main top-level master actor here ...


      }

}

The contextInitialized gets called by Tomcat on the application's start up. Now the question is, let's say I have a top level actor who may be escalated a failure from one of his children. I want this master actor to essentially log the failure that happened and shutdown the JVM afterwards. How can this be accomplished in a clean way?

The clean way of shutting down an Akka application may be:

1) Stop getting incoming request from the outside world (depends on your implementation);

2) Send PoisonPill -s to all your top-level actors in the correct order (exact or similar to your main data flow in your app; also your routers should be able to broadcast PoisonPill -s to their routees);

3) Use the Reaper pattern to watch your actors and decide that all of them have processed all their messages and stopped and after that stop the ActorSystem (don't forget a reasonably high timeout for actors stopping to stop the ActorSystem in any case);

4) Await for the ActorSystem termination ( ActorSystem.awaitTermination(scala.concurrent.duration.Duration timeout) );

5) Shutdown all other parts of your app which should be properly terminated;

6) Shutdown the JVM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM