1

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 shutsdown after x tries.

Problem:

I need to stop Apache Tomcat after the webapp 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 shutdown if the only web application shuts down - I could not find a way to do that

Any ideas? Maybe a different approach?

regards

Alexander

Dynamicbyte
  • 541
  • 1
  • 5
  • 13

3 Answers3

1

When Tomcat is started, you essentially invoke org.apache.catalina.startup.Bootstrap::main with parameter start. To stop Tomcat, invoke the same class/method with command stop. You should not need another Java process for that, just call the main method statically.

See the Javadoc: http://tomcat.apache.org/tomcat-5.5-doc/catalina/docs/api/org/apache/catalina/startup/Bootstrap.html

If you don't want the dependency on Tomcat libs, just do some reflection magic.

mindas
  • 26,463
  • 15
  • 97
  • 154
  • I was thinking about that already, but how do I get the Bootstrap object when I am within the webb application? Outside the web application I dont know that the db connection does not work. – Dynamicbyte Nov 16 '10 at 10:40
  • As I mentioned, just use static main method - org.apache.catalina.startup.Bootstrap.main(new String[] {"stop"}) – mindas Nov 16 '10 at 11:44
  • From within the running web app it does not work! The Bootstrap object can be called but an exception ("Connection refused") is thrown. I know that it works from a different Java process, but I wanted to avoid having to call an external application. – Dynamicbyte Nov 19 '10 at 10:23
1

OK, I'm pretty sure this is a bad way of doing it, but like you, I have been unable to come up with a better solution.

try{
    MBeanServer server = MBeanUtils.createServer();
    ObjectName name = new ObjectName("Catalina:type=Service,serviceName=Catalina");
    server.invoke(name, "stop", new Object[0], new String[0]);
} catch (Exception) {
     e.printStackTrace();
}

This will basically tell Catalina to kill itself.

Andrew
  • 13,757
  • 13
  • 66
  • 84
1

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

Community
  • 1
  • 1
prunge
  • 22,460
  • 3
  • 73
  • 80