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:
- Stop Apache Tomcat from within the web application (already tried the shutdown port which did not work because the connection was refused – with a standalone Java application it worked)
- Call an external Java application from within the web application
- Configure Apache Tomcat to shut down if the only web application shuts down – I could not find a way to do that
Any ideas? Maybe a different approach?
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