tomcattomcat6

Stop Apache Tomcat when web application stops


Scenario:

Apache Tomcat 6.0 is started as a service on Windows Server 2008 R2 using a wrapper (org.tanukisoftware.wrapper.WrapperStartStop) which uses org.apache.catalina.startup.Bootstrap. In course of the Tomcat startup one web application is also started. This web application needs to connect to a remote database and check the connection. It retries to connect a couple of times if the database is not available and then shutdown after x tries.

Problem:

I need to stop Apache Tomcat after the web app exits when the database connection is not available.

Possible solutions:

Any ideas? Maybe a different approach?


Solution

  • System.exit(0) from within your webapp will shut down the Tomcat instance if there is no security manager configured.

    Works from a standalone server, not sure whether it works when running as a Windows service.

    Edit: though you might want to read this: Calling System.exit() in Servlet's destroy() method