0

Is there a way to stop tomcat's startup if certain conditions don't occur?

For example, I'd like the contextInitialized method of a servletContextListener to check for various property file attributes as well as verify database connection, etc.

I've seen ways to shutdown tomcat via an MBean event but it requires the host, shutdown port. In my case I am hoping to have a single base class that can be used in multiple tomcats on the same server (each with their own shutdown port).

Fred
  • 335
  • 1
  • 6
  • 22
  • It's an old answer but could still work, also uses an MBean but I don't see a host or portnumber: https://stackoverflow.com/a/4471672/3080094 – vanOekel Oct 27 '19 at 23:01

1 Answers1

0

You could call Tomcat's shutdown port (typically 8005)... All you need is the port number and the text (e.g. 8005 and SHUTDOWN). Given you are on the host (the servletContextListener is running within Tomcat), you don't need a hostname. Localhost should do the trick.

An alternative is to have your Java code run a process on the server - invoke ./shutdown.sh but that will involve knowing where that script is.

David Brossard
  • 13,584
  • 6
  • 55
  • 88
  • 1
    thank you for the suggestion. I am hung up though on the port number. My design goal is to have a standard class that is used in five different webapps and the class has the contextListener autoWired so it starts up on its own without any customization to each of the web.xml (the ports may be different for each app across dev, test & prod servers). The goal is to have the class fully self-managing so that if something during setup fails it'll churn out a useful error message to the log and then shutdown tomcat. – Fred Oct 28 '19 at 12:34
  • Look into whether you can determine CATALINA_HOME from your code. If you can, then you can invoke ./shutdown.sh – David Brossard Oct 28 '19 at 12:40