简体   繁体   English

在Web应用程序中正确关闭Akka actor系统

[英]Proper shutdown of Akka actor system in web application

I'm using Akka in one of my web applications. 我在一个Web应用程序中使用Akka。 The application is a standard Spring app that gets initialized in the Tomcat servlet container. 该应用程序是在Tomcat Servlet容器中初始化的标准Spring应用程序。

I have a simple ServletContextListener like this: 我有一个简单的ServletContextListener像这样:

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. Tomcat在应用程序启动时调用contextInitialized。 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. 我希望该主要角色本质上记录发生的故障并随后关闭JVM。 How can this be accomplished in a clean way? 怎样才能做到这一点呢?

The clean way of shutting down an Akka application may be: 关闭Akka应用程序的干净方法可能是:

1) Stop getting incoming request from the outside world (depends on your implementation); 1)停止接收来自外界的传入请求(取决于您的实现);

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); 2)以正确的顺序将PoisonPill -s发送给所有顶级角色(与您应用中的主要数据流完全相同或相似;路由器也应该能够将PoisonPill -s广播到其路由);

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); 3)使用“ 收割者”模式来监视您的演员,并确定他们已经处理了所有消息并已停止,然后又停止了ActorSystem(在任何情况下都不要忘记让演员停止停止ActorSystem的超时时间相当长);

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

5) Shutdown all other parts of your app which should be properly terminated; 5)关闭应正确终止的应用程序的所有其他部分;

6) Shutdown the JVM. 6)关闭JVM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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