按照本教程配置jvm
模式 Java Windows 服务:(https://joerglenhard.wordpress.com/2012/05/29/build-windows-service-from-java-application-with-procrun/)。我在 start 和 stop 方法中按线程 ID 将日志消息打印到文件中,如下所示:
private static boolean stop = false;
public static void main( String[] args )
{
log.debug(Integer.toHexString(System.identityHashCode(Thread.currentThread())));
if (args.length == 0) {
log.debug("no args provided, give start/stop as argument");
return;
}
String mode = args[0];
if ("start".equals(mode)) {
log.debug("start " + Integer.toHexString(System.identityHashCode(Thread.currentThread())));
startService(args);
} else if ("stop".equals(mode)) {
log.debug("stop " + Integer.toHexString(System.identityHashCode(Thread.currentThread())));
stopService(args);
}
log.debug("End of main " + Integer.toHexString(System.identityHashCode(Thread.currentThread())));
}
这是显示服务启动和停止的日志输出:
22/Aug/2016 19:22:00,962- App: 441772e
22/Aug/2016 19:22:00,962- App: start 441772e
22/Aug/2016 19:22:00,962- App: startService
22/Aug/2016 19:23:21,259- App: 1ef37254
22/Aug/2016 19:23:21,259- App: stop 1ef37254
22/Aug/2016 19:23:21,259- App: stopService
22/Aug/2016 19:23:21,259- App: End of main 1ef37254
22/Aug/2016 19:23:22,181- App: End of main 441772e
线程 ID 出现在日志文件中,表示启动服务和停止服务的新进程已启动。即使变量stop
是private static boolean
日志文件,也表明服务是不同的进程(对吗?)。那么,为什么要创建多个 Windows 进程来启动和停止我的服务?