有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Tomcat服务在停止使用JNA时引发运行时异常

我试图在Java程序中停止Tomcat服务和一个使用JNA的定制服务(我在windows中创建的)。有时Tomcat服务需要更长的时间才能停止

 W32Service service = null;
        long startTime = System.currentTimeMillis();
        boolean isStopped = false;
        try
        {
            W32ServiceManager serviceManager = new W32ServiceManager();
            serviceManager.open(Winsvc.SC_MANAGER_ALL_ACCESS); 
            service = serviceManager.openService(serviceName, Winsvc.SC_MANAGER_ALL_ACCESS);
            //service.queryStatus().dwWaitHint =  60000;
            synchronized (service) {
             service.stopService();
            }
            service.close();
        } catch (Exception ex)
        {
            System.out.println("Timeout happened.");

        }finally{
            if(null != service.getHandle()){
                while(!isStopped){
                    if(service.queryStatus().dwCurrentState == Winsvc.SERVICE_STOPPED
                            || System.currentTimeMillis()-startTime >= Constants.SERVICE_STOP_WAIT_TIME){
                        isStopped=true;
                    }
                }

                if(System.currentTimeMillis()-startTime >= Constants.SERVICE_STOP_WAIT_TIME){
                    System.out.println("Service Continuing.....");
                    throw new ServiceNotStoppedException("Service Not Stopped after 2 minutes");

                }else{
                    System.out.println("Service Stopped");
                    isStopped=true;
                }
            }else{
                System.out.println("Service Stopped.");
                isStopped=true;
            }
        }

在上面的代码中,我捕获了一个抛出的运行时异常,它给出了下面的消息

 java.lang.RuntimeException: Timeout waiting for service to change to a non-pending state.
at com.sun.jna.platform.win32.W32Service.waitForNonPendingState(W32Service.java:162)
at com.sun.jna.platform.win32.W32Service.stopService(W32Service.java:98)
at com.taskkill.sample.Sample.stopService(Sample.java:32)
at com.taskkill.sample.Sample.main(Sample.java:12)

我已经查看了之前关于类似主题的帖子,但找不到任何解决方案。它正在等待默认时间并抛出错误。我们有没有办法延长这段时间?(我试过//service.queryStatus()。dwWaitHint=60000;)但它不起作用。使用此方法时,Tomcat服务最终会停止/挂起。这是可行的,但是有更好的方法来处理RunTimeException吗

如果我通过以下代码使用命令提示符进程,它会立即返回退出代码“0”,而进程仍在后台停止

String[] stopTomcat = { "sc", "stop", "Tomcat7" };
Process tomcatProcess = Runtime.getRuntime().exec(stopTomcat); 

共 (0) 个答案