我正在使用它将我的应用程序安装为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: stopped“,但Window控制面板服务小程序显示”正在停止服务...“,但它从来没有出现过。
是什么让服务停止,即使我确定它已经没有错误地退出了serviceMain()?
发布于 2015-01-12 05:42:49
我不知道你是否能解决它,但我有一个类似的问题,我通过添加一个名为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;
}https://stackoverflow.com/questions/23129919
复制相似问题