WinRun4J - 服务没有停止

时间:2014-04-17 09:53:37

标签: winrun4j

我使用它将我的应用程序安装为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:已停止"在我的日志中,窗口控制面板服务小程序只是坐着说#34;停止服务...",但它永远不会。

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

1 个答案:

答案 0 :(得分: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;
}
相关问题