如何将守护进程实现设置为Windows服务 [英] Howto setup a Daemon implementation as windows service

查看:205
本文介绍了如何将守护进程实现设置为Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到任何关于如何注册实现 org.apache.commons.daemon.Daemon 接口作为Windows服务。

I didn't find any really good example (actually I did not find a single example) on how to register a class that implements the org.apache.commons.daemon.Daemon interface as Windows service.

我是否必须使用procrun注册此实现?但是实现接口似乎没有意义,因为procrun可以将任何程序注册为Windows服务。

Do I have to register this implementation using procrun? But than there doesn't seem to be a point in implementing the interface as procrun can register any program as windows service.

此外,似乎有一个文档错误procrun页面( http://commons.apache.org/proper/commons-daemon /procrun.html ):

Furthermore there seems to be a docu-bug on the procrun page (http://commons.apache.org/proper/commons-daemon/procrun.html):

- StartMethod 参数说明:


注意:在jvm模式下,start方法不应该在调用stop方法之前返回。

Note: in jvm mode, the start method should not return until the stop method has been called.

但是在在jvm模式下使用Procrun部分的页面下方:

But further down the page in the "Using Procrun in jvm mode" section:


注意方法处理服务启动应该创建并启动一个单独的线程来进行处理,然后返回。从不同的线程调用start和stop方法。

Note that the method handling service start should create and start a separate thread to carry out the processing, and then return. The start and stop methods are called from different threads.

我读错了还是有点矛盾?什么是静态启动(String [] args)方法的正确行为?

Am I reading this wrong or is this a little conflicting?? And what would be the correct behavior of a static start(String[] args) method?

祝你好运

推荐答案

对于记录:


我是否必须使用procrun注册此实现?但是实现接口似乎没有意义,因为procrun可以将任何程序注册为Windows服务。

Do I have to register this implementation using procrun? But than there doesn't seem to be a point in implementing the interface as procrun can register any program as windows service.

是的需要使用prunsrv在Windows中注册服务。例如,使用以下调用:

Yes the service needs to be registered in Windows using prunsrv. For example with the following call:

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

此后服务可以启动by

After this the service can be started by

prunsrv //ES//MyTestSevice



静态启动的正确行为是什么(String [ ] args)方法?

And what would be the correct behavior of a static start(String[] args) method?

Testi两个变体,只有实现工作,保持在启动方法,并没有产生额外的线程。这是一个可以通过上面的prunsrv调用注册的启动器实现看起来像这样(没有任何保证):

Testing both variants, only the implementation worked, that stayed in the start-method an did not spawn additional threads. That is a launcher implementation that can be registered with the above prunsrv call would look something like this (without any warranty):

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();
      }
    }
  }
}

这篇关于如何将守护进程实现设置为Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆