记录:
我是否必须使用procrun注册该实现?但是实现该接口似乎没有意义,因为procrun可以将任何程序注册为Windows服务。
是的,该服务需要使用prunsrv在Windows中注册。例如,使用以下调用:
prunsrv.exe //IS//MyTestService ^ --DisplayName="My Test Service" --Description="Doesn't really do anything" ^ --Install=@@PATH_TO_PRUNSRV@@prunsrv.exe ^ --Startup=manual ^ --Jvm=auto ^ --Classpath="@@PUT_FULL_CLASSPATH_HERE@@" ^ --StartMode=jvm ^ --StartClass==com.stackoverflow.questions.31556478.ServiceLauncher ^ --StartParams="@@PUT_ANY_START_ARGUMENTS_HERE@@" ^ --StartMethod=start ^ --StopMode=jvm ^ --StopClass=com.stackoverflow.questions.31556478.ServiceLauncher ^ --StopMethod=stop
之后,可以通过以下方式启动服务
prunsrv //ES//MyTestSevice
静态start(String [] args)方法的正确行为是什么?
测试这两种变体,只有实现可行,并且停留在启动方法中,并且不会产生其他线程。那是一个可以在上面的prunsrv调用中注册的启动器实现,看起来像这样(没有任何保证):
package com.stackoverflow.questions.31556478;import java.util.Arrays;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class ServiceLauncher{ private static final Logger LOGGER = LoggerFactory.getLogger(ServiceLauncher.class); private static SomeServer mServer; public static void start(final String[] args) { LOGGER.debug("Start called: {}", Arrays.toString(args)); try { mServer = new SomeServer(args); mServer.start(); } catch (final Exception e) { LOGGER.error("Terminating due to Exception: ", e); } } public static void stop(final String[] args) throws Exception { LOGGER.debug("Stop called: {}", Arrays.toString(args)); synchronized (ServiceLauncher.class) { if (mServer != null) { mServer.stop(); } } }}


