开发者

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 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开发者_如何学Python the only web application shuts down - I could not find a way to do that

Any ideas? Maybe a different approach?

regards

Alexander


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜