作为windows服务运行的嵌入式tomcat需要很长时间才能停止服务

问题描述

我有一个使用嵌入式 tomcat(9.0.44) 的可执行 jar 文件。并且它作为带有 apache prunsrv 实用程序的 Windows 服务(名为“MyApp 测试服务”)运行。 但是当我尝试停止服务时,停止服务需要一些时间(超过一分钟)。但是启动服务非常快。 我可以确认 tomcat 的 stop() 方法很快完成。我怀疑 prunsrv 中还有其他东西等待并需要时间来停止服务。请帮助了解发生了什么以及如何解决此问题(执行 tomcat.stop() 后立即停止服务)

  • 注册服务 - C:\ServiceTest\prunsrv.exe" "//RS//MyApp Test Service"
  • 启动类和方法com.samples.myapp.TestembeddedServer::main
  • 关闭类和方法com.samples.myapp.TestembeddedServer::stop

Shutdown & Startup tags in the prunmgr UI

TomcatEmbeddedServer .java

public class TomcatEmbeddedServer {
    private int port;
    private Tomcat tomcat;
    private Context context;

    private static final Logger LOGGER = LoggerFactory.getLogger(TomcatEmbeddedServer.class);

    public TomcatEmbeddedServer(int port,String contextName,String tomcatPath,String webAppPath) {
        System.setProperty("org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH","true");
        System.setProperty("org.apache.catalina.connector.CoyoteAdapter.ALLOW_BACKSLASH","true");

        this.port = port;

        LOGGER.info("Port: {}",port);
        LOGGER.info("Context: {}",contextName);
        LOGGER.info("Tomcat Path: {}",tomcatPath);
        LOGGER.info("Webapp Path: {}",webAppPath);

        // Prepare tomcat server
        this.tomcat = new Tomcat();
        this.tomcat.setBaseDir(tomcatPath);

        // Set up the context
        this.context = this.tomcat.addWebapp(contextName,webAppPath);
        this.context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
    }

    public void start() throws LifecycleException {

        Connector connector = this.tomcat.getConnector();
        connector.setEncodedSolidusHandling(EncodedSolidusHandling.DECODE.getValue());
        connector.setPort(this.port);

        LOGGER.info("Starting tomcat server ...{}",this.tomcat);
        this.tomcat.start();
        this.tomcat.getServer().await();
    }

    public void stop() throws LifecycleException {
        this.tomcat.stop();
    }
}

TestembeddedServer.java

public class TestembeddedServer {

    private static TomcatEmbeddedServer tomcat;
    private static Logger log = LoggerFactory.getLogger(TestembeddedServer.class);

    public static void main(String[] args) {
        String tomcatDir = "D:/testserver/tomcat";
        String webAppDir = "D:/testserver/mysampleapp";
        String context = "/sampleapp";
        int port = 8090;

        try
        {
            tomcat = new TomcatEmbeddedServer(port,context,tomcatDir,webAppDir);
            tomcat.start();
        } catch (Exception e) {
            log.error("Failed to start tomcat server.",e);
        }
    }

    public static void stop(String[] args) {
        try {
            tomcat.stop();
        } catch (LifecycleException e) {
            log.error("Failed to stop tomcat.",e);
        }
    }
}

以下是我获得的 prunserv 启动/关闭日志

[2021-06-02 10:42:36] [debug] ( prunsrv.c:1729) [85696] Commons Daemon procrun log initialized
[2021-06-02 10:42:36] [info]  ( prunsrv.c:1733) [85696] Commons Daemon procrun (1.1.0.0 64-bit) started
[2021-06-02 10:42:36] [info]  ( prunsrv.c:1643) [85696] Running 'MyApp Test Service' Service...
[2021-06-02 10:42:36] [debug] ( prunsrv.c:1417) [85736] Inside ServiceMain...
[2021-06-02 10:42:36] [debug] ( prunsrv.c:885 ) [85736] reportServiceStatusE: dwCurrentState = 2,dwWin32ExitCode = 0,dwWaitHint = 3000,dwServiceSpecificExitCode = 0
[2021-06-02 10:42:36] [info]  ( prunsrv.c:1175) [85736] Starting service...
[2021-06-02 10:42:36] [debug] ( javajni.c:236 ) [85736] loading jvm 'C:\Program Files\Java\bin\server\jvm.dll'
[2021-06-02 10:42:36] [debug] ( javajni.c:753 ) [85744] Jvm Option[0] -Dfile.encoding=UTF8
[2021-06-02 10:42:36] [debug] ( javajni.c:753 ) [85744] Jvm Option[1] --add-opens=java.base/java.lang=ALL-UNNAMED
[2021-06-02 10:42:36] [debug] ( javajni.c:753 ) [85744] Jvm Option[2] --add-opens=java.base/java.io=ALL-UNNAMED
[2021-06-02 10:42:36] [debug] ( javajni.c:753 ) [85744] Jvm Option[3] --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
[2021-06-02 10:42:36] [debug] ( javajni.c:753 ) [85744] Jvm Option[4] --illegal-access=permit
[2021-06-02 10:42:36] [debug] ( javajni.c:753 ) [85744] Jvm Option[5] exit
[2021-06-02 10:42:36] [debug] ( javajni.c:753 ) [85744] Jvm Option[6] abort
[2021-06-02 10:42:39] [debug] ( javajni.c:990 ) [85744] Java Worker thread started com/samples/myapp/TestembeddedServer:main
[2021-06-02 10:42:40] [debug] ( prunsrv.c:1235) [85736] Java started com/samples/myapp/TestembeddedServer
[2021-06-02 10:42:40] [info]  ( prunsrv.c:1333) [85736] Service started in 4102 ms.
[2021-06-02 10:42:40] [debug] ( prunsrv.c:885 ) [85736] reportServiceStatusE: dwCurrentState = 4,dwWaitHint = 0,dwServiceSpecificExitCode = 0
[2021-06-02 10:42:40] [debug] ( prunsrv.c:1572) [85736] Waiting for worker to finish...
[2021-06-02 10:43:38] [debug] ( prunsrv.c:885 ) [85696] reportServiceStatusE: dwCurrentState = 3,dwServiceSpecificExitCode = 0
[2021-06-02 10:43:39] [info]  ( prunsrv.c:984 ) [85296] Stopping service...
[2021-06-02 10:43:40] [debug] ( javajni.c:990 ) [85488] Java Worker thread started com/samples/myapp/TestembeddedServer:stop
[2021-06-02 10:43:41] [debug] ( prunsrv.c:1032) [85296] Waiting for Java JNI stop worker to finish...
[2021-06-02 10:43:48] [debug] ( javajni.c:1013) [85744] Java Worker thread finished com/samples/myapp/TestembeddedServer:main with status = 0
[2021-06-02 10:43:48] [debug] ( javajni.c:1013) [85488] Java Worker thread finished com/samples/myapp/TestembeddedServer:stop with status = 0
[2021-06-02 10:43:48] [debug] ( prunsrv.c:1034) [85296] Java JNI stop worker finished.
[2021-06-02 10:43:48] [debug] ( prunsrv.c:885 ) [85296] reportServiceStatusE: dwCurrentState = 3,dwWaitHint = 5000,dwServiceSpecificExitCode = 0
[2021-06-02 10:43:48] [debug] ( prunsrv.c:1141) [85296] Waiting for worker to die naturally...
[2021-06-02 10:43:48] [debug] ( prunsrv.c:1152) [85296] Worker finished gracefully in 0 ms.
[2021-06-02 10:43:48] [info]  ( prunsrv.c:1162) [85296] Service stop thread completed.
[2021-06-02 10:43:48] [debug] ( prunsrv.c:1577) [85736] Worker finished.
[2021-06-02 10:43:48] [debug] ( prunsrv.c:1586) [85736] Waiting for ShutdownEvent
[2021-06-02 10:43:48] [debug] ( prunsrv.c:885 ) [85736] reportServiceStatusE: dwCurrentState = 3,dwWaitHint = 60000,dwServiceSpecificExitCode = 0
[2021-06-02 10:43:48] [debug] ( prunsrv.c:1589) [85736] ShutdownEvent signaled
[2021-06-02 10:43:48] [debug] ( prunsrv.c:1594) [85736] Waiting 1 minute for all threads to exit
[2021-06-02 10:43:48] [debug] ( prunsrv.c:885 ) [85736] reportServiceStatusE: dwCurrentState = 3,dwServiceSpecificExitCode = 0
[2021-06-02 10:44:48] [debug] ( prunsrv.c:1607) [85736] JVM destroyed.
[2021-06-02 10:44:48] [debug] ( prunsrv.c:885 ) [85736] reportServiceStatusE: dwCurrentState = 1,dwServiceSpecificExitCode = 0
[2021-06-02 10:44:48] [info]  ( prunsrv.c:1645) [85696] Run service finished.
[2021-06-02 10:44:48] [info]  ( prunsrv.c:1814) [85696] Commons Daemon procrun finished

关闭信号后的这些日志来看,销毁 JVM 的延迟正好是 1 分钟。我认为这就是它挂的地方。

[2021-06-02 10:43:48] [debug] ( prunsrv.c:1589) [85736] ShutdownEvent signaled
[2021-06-02 10:43:48] [debug] ( prunsrv.c:1594) [85736] Waiting 1 minute for all threads to exit
[2021-06-02 10:43:48] [debug] ( prunsrv.c:885 ) [85736] reportServiceStatusE: dwCurrentState = 3,dwServiceSpecificExitCode = 0
[2021-06-02 10:44:48] [debug] ( prunsrv.c:1607) [85736] JVM destroyed.

在 prunmngr UI 中,进度指示器已关闭,但开始按钮未启用。请参阅下面的屏幕截图。

GUI after the stop service.. progress bar dismissed but button not enabled

编辑:改变 Tomcat 版本的结果

使用的Tomcat版本 停止时间
Apache Tomcat/8.5.66 ~9 秒
Apache Tomcat/9.0.1 ~9 秒
Apache Tomcat/9.0.10 ~9 秒
Apache Tomcat/9.0.13 ~9 秒
Apache Tomcat/9.0.14 ~ 1 分 3 秒
Apache Tomcat/9.0.16 ~ 1 分 3 秒
Apache Tomcat/9.0.20 ~ 1 分 3 秒
Apache Tomcat/9.0.30 ~ 1 分 3 秒
Apache Tomcat/9.0.40 ~ 1 分 3 秒
Apache Tomcat/9.0.44 ~ 1 分 3 秒
Apache Tomcat/9.0.46 ~ 1 分 3 秒
Apache Tomcat/10.0.6 ~ 1 分 3 秒

解决方法

自 Tomcat 版本 9.0.14 起引入了实用程序执行器:

向服务器添加一个调度执行器,可以用来处理周期性的实用程序任务。默认情况下,实用程序线程是非守护进程。 (remm)

它的线程有意非守护进程,因此服务器 stop() 不会关闭 JVM。要完全停止服务器,您必须使用 destroy():

    public void stop() throws LifecycleException {
        this.tomcat.stop();
        this.tomcat.destroy();
    }