2

我正在使用它来将我的应用程序安装为 Windows 服务。一切正常,只是服务没有停止;

@Override
public int serviceMain(String[] strings) throws ServiceException {        

    try {
        System.out.println("BootService: init");
        System.out.println("BootService: service loop start");            
        while (ws.isServiceRunning()) {
            System.out.println("BootService: loop");
            ws.serviceHandler();
        }

        System.out.println("BootService: stopped");       
        return 0;

    } catch (Exception ex) {            
        throw new ServiceException(ex);
    }

}

@Override
public int serviceRequest(int control) throws ServiceException {
    try {
        switch (control) {
            case SERVICE_CONTROL_SHUTDOWN:
            case SERVICE_CONTROL_STOP:
                if (ws!=null) {
                    ws.stopService();
                }
                break;
        }
        return 0;
    } catch (WindowsServiceException ex) {
        throw new ServiceException(ex);
    }
}

我的服务后端代码被对 serviceRequest() 的调用停止,这反过来又使 serviceMain() 中的循环退出。我在日志中看到消息“BootService:已停止”,但窗口控制面板服务小程序只是显示“正在停止服务...”,但它从来没有。

即使我确定它已经退出 serviceMain() 没有错误,什么会阻止服务停止?

4

1 回答 1

1

我不知道你能不能解决它,但我有一个类似的问题,我通过添加一个名为 System.exit(0) 的计时器来解决它

public int serviceMain(String[] args) throws ServiceException {
    while (!shutdown) {
        try {
            if (!myservice.isRunning()) {
                (new Thread(new LaucherRunnable(args))).start();
            }
            Thread.sleep(6000);
        } catch (InterruptedException e) {
        }         
    }
    periodicRunner.stop();
    Timer t = new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    t.setRepeats(false);
    t.start();
    return 0;
}
于 2015-01-11T21:42:49.137 回答