简体   繁体   English

从 Servlet 关闭/取消部署 tomcat

[英]shutdown / undeploy tomcat from Servlet

I have an init servlet in Tomcat that loads critical data.我在 Tomcat 中有一个加载关键数据的 init servlet。 Sometimes it is necessary to abort startup on certain errors.有时有必要因某些错误而中止启动。

how do i gracefully shutdown the deployed app/ the whole app server without calling System.exit(1)如何在不调用System.exit(1)情况下优雅地关闭已部署的应用程序/整个应用程序服务器

i want to avoid calling the shutdown servlet via port, since this is not configured in my installation.我想避免通过端口调用关闭 servlet,因为这在我的安装中没有配置。

there may be tasks that need to be run from listeners on shutdown defined in web.xml可能有一些任务需要在 web.xml 中定义的关闭时从侦听器运行

First of all, you should never ever ever ever call System.exit() from within a servlet container, as there might be other applications running within the same process as yours, and it would be incredibly incorrect to forcibly kill the entire process.首先,您永远不应该从 servlet 容器内调用System.exit() ,因为可能有其他应用程序在与您的进程相同的进程中运行,并且强行终止整个进程是非常不正确的。

Assuming your "init servlet" implements ServletContextListener , there is no formal way defined in the API/interface to signal to the servlet container "please shut down the app neatly".假设您的“init servlet”实现了ServletContextListener ,API/接口中没有定义正式的方法来向 servlet 容器发出“请彻底关闭应用程序”的信号。 You could however throw some sort of RuntimeException from the contextInitialized() method, which in most containers (at least Tomcat) will stop the startup of your webapp and leave it in the "stopped" state.但是,您可以从contextInitialized()方法中抛出某种RuntimeException ,这在大多数容器(至少 Tomcat)中将停止您的 web 应用程序的启动并使其处于“停止”状态。 However I doubt that Tomcat will continue to call your shutdown listeners.但是我怀疑 Tomcat 是否会继续调用您的关闭侦听器。

You might want to rethink your design so that your critical data / shutdown logic is not tied so tightly with the lifecycle of the servlet container.您可能需要重新考虑您的设计,以便您的关键数据/关闭逻辑不会与 servlet 容器的生命周期如此紧密地联系在一起。

What I added was a healthcheck probe that would check if the server was still up using a curl I think it can be modified to check if a "flag" file is present as well.我添加的是一个健康检查探针,它会使用curl检查服务器是否仍在运行,我认为可以修改它以检查是否还存在“标志”文件。 I just send a catalina.sh stop command.我只是发送一个catalina.sh stop命令。

If this was a Docker container, you can use the healthcheck probe to let it close itself as well.如果这是一个 Docker 容器,您也可以使用 healthcheck 探针让它自行关闭。

Ideally if you can migrate to something like Spring Boot which runs tomcat but terminates it if the Spring Context does not load properly.理想情况下,如果您可以迁移到 Spring Boot 之类的东西,它运行 tomcat,但如果 Spring Context 没有正确加载则终止它。

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

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