I would like to run some code, in my webapplication, to cause the application to stop. For example, if the database server is unavailable.
I would like to implement something like App.exit()
similar to System.exit()
.
FYI
The answer in "Shutdown tomcat using web application deployed in it" is about shutting down the whole container. The question here is about shutting down (or temporary disabling) a single web application.
Use a browser to check whether Tomcat is running on URL http://localhost:8080 , where 8080 is the Tomcat port specified in conf/server. xml. If Tomcat is running properly and you specified the correct port, the browser displays the Tomcat homepage.
Some other application is binding to port 8005 which is the default Apache Tomcat shutdown port. Because it is in use, Tomcat cannot bind to this port during startup, causing it to fail startup.
You can shutdown a single application in Tomcat, using MBeans/JMX:
public void shutdownApp() {
try {
String serviceName = "Catalina"; // @see server.xml
String hostName = "localhost"; // @see server.xml
String contextName = "MyApplicationName"; // the name of your application in the URL
Hashtable<String, String> keys = new Hashtable<>();
keys.put("j2eeType", "WebModule");
keys.put("name", "//" + hostName + "/" + contextName);
keys.put("J2EEApplication", "none");
keys.put("J2EEServer", "none");
MBeanServerConnection mbeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName appObject = ObjectName.getInstance(serviceName, keys);
System.out.println("Found objectName: " + appObject);
mbeanServer.invoke(appObject, "stop", null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
The serviceName, hostName and contextName vars need to be changed according to your configuration. This example will shutdown an app that is deployed like this:
hxxp://localhost:8080/MyApplicationName
Beside "stop" you may also call: "start" or "reload".
Usually we dont call App.exit or System.exit in a web application. If Database server is not avilable only the reason or kind of similar issue you can do the following
Also using tomcat manager you can start/stop/deploy any webapps in your tomcat container.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With