Proper shutdown of Akka actor system in web applic

2019-04-16 06:54发布

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?

1条回答
家丑人穷心不美
2楼-- · 2019-04-16 07:17

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.

查看更多
登录 后发表回答