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

编程入门 行业动态 更新时间:2024-10-14 10:38:06
本文介绍了在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系统

本文发布于:2023-11-25 04:50:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628407.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   正确   系统   Web   actor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!