如何关闭Tomcat中的单个应用程序? [英] How to shutdown a single application in Tomcat?

查看:62
本文介绍了如何关闭Tomcat中的单个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 web 应用程序中运行一些代码,使应用程序停止.例如,如果数据库服务器不可用.我想实现类似于 System.exit() 的类似 App.exit() 的东西.

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().

仅供参考

使用部署在其中的 Web 应用程序关闭 tomcat"中的答案是关于关闭整个容器.这里的问题是关于关闭(或暂时禁用)单个网络应用程序.

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.

推荐答案

您可以使用 MBeans/JMX 关闭 Tomcat 中的单个应用程序:

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();
    }
}

serviceName、hostName 和 contextName 变量需要根据您的配置进行更改.此示例将关闭这样部署的应用程序:

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".

这篇关于如何关闭Tomcat中的单个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆