views:

359

answers:

1

I have an init servlet in Tomcat that loads critical data. soemtimes 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)

i want to avoid calling the shutdown servlet via port, since this is not configured in my installation.

there may be tasks that need to be run from listeners on shutdown defined in web.xml

+1  A: 

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.

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". 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. However I doubt that Tomcat will continue to call your shutdown listeners.

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.

matt b
its more about shutting down utility threads and correctly writing logs
Andreas Petersson
I used to share a Tomcat with a bad app which runs out of memory all the time. When I detect that, I call System.exit(0). The shutdown is as graceful as other approach in Tomcat.
ZZ Coder
Well if you're getting OOMs then at that point there's nothing left to do but shutdown the JVM.
matt b