Shutdown / undeploy tomcat from servlet

I have an init servlet in Tomcat that loads critical data. Sometimes you have to interrupt the launch for certain errors.

how to gracefully shut down a deployed application / entire application server without calling System.exit(1)

I want to avoid calling the shutdown servlet through the port, as this is not configured in my installation.

there may be tasks that need to be run from listeners during shutdowns defined in web.xml

+4
source share
1 answer

First of all, you should never ever call System.exit() from the servlet container, as there may be other applications running in the same process as yours, and it would be incredibly incorrect to forcefully kill the whole process.

Assuming that your "init servlet" implements ServletContextListener , there is no formal signal defined in the API / interface to signal the servlet container "please shut down the application carefully." You could, however, throw a RuntimeException from the contextInitialized() method, which in most containers (at least Tomcat) will stop your webapp from starting and leave it in a stopped state. However, I doubt that Tomcat will continue to call shutdown listeners.

You might want to rethink your design so that your critical data / shutdown logic is not closely related to the servlet container life cycle.

+7
source

All Articles