我正在使用它来将我的应用程序安装为 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() 没有错误,什么会阻止服务停止?