在Web应用程序中正确关闭Akka actor系统 [英] Proper shutdown of Akka actor system in web application

查看:269
本文介绍了在Web应用程序中正确关闭Akka actor系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个网络应用程序中使用Akka。该应用程序是一个标准的Spring应用程序,它在Tomcat servlet容器中初始化。

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.

我有一个简单的ServletContextListener,如下所示:

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 ...


      }

}

在应用程序启动时,Tomcat会调用contextInitialized。
现在问题是,假设我有一名顶级演员可能会因为他的一个孩子而失败。我希望这个主操作员基本上记录发生的故障,然后关闭JVM。如何以一种干净的方式实现这一目标?

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?

推荐答案

关闭Akka应用程序的简洁方法可能是:

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

1)停止接收来自外部世界的请求(取决于您的实施);

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

2)发送 PoisonPill -s以正确的顺序显示所有顶级演员(与您应用中的主要数据流完全相似;您的路由器也应该能够广播 PoisonPill -s到他们的路线);

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)使用收割者模式,观察你的演员,并决定他们所有人都处理了他们所有的消息并停止了,之后停止了演员系统(不要忘记一个相当高的在任何情况下,演员停止停止ActorSystem的超时);

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)等待ActorSystem终止( ActorSystem.awaitTermination(scala.concurrent。 duration.Duration时间eout));

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

5)关闭应用程序中应该正确终止的所有其他部分;

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

6)关闭JVM。

这篇关于在Web应用程序中正确关闭Akka actor系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆