简体   繁体   English

如何使用winrun4j创建Windows服务

[英]How to create a windows service with winrun4j

I've been reading the documentation but I can't start and stop de service. 我一直在阅读文档,但我无法启动和停止服务。

My .ini file is: 我的.ini文件是:

main.class=test.TestService
service.class=test.TestService
service.id=StreamServer
service.name=StreamServer
service.description=Servidor que proporciona una comunicación con streams.
service.controls=stop   
classpath.1=*.jar

The TestService class is: TestService类是:

package test;

public class TestService{
    private static TestServer server;

    public static void main (String[] args){
        if (args.length == 1){
            if (args[0].equals ("start")){
                if (server == null){
                    server = new TestServer (5000);
                    server.start ();
                }
            }else if (args[0].equals ("stop")){
                if (server != null){
                    server.stop ();
                    server = null;
                }
            }
        }
    }
}

I have to modify this class but I don't know how. 我必须修改这个类,但我不知道如何。

Thanks. 谢谢。

Take a look at the sample service from the front page of the winrun4j site: 从winrun4j网站的首页看一下示例服务:

package org.boris.winrun4j.test;

import org.boris.winrun4j.AbstractService;
import org.boris.winrun4j.EventLog;
import org.boris.winrun4j.ServiceException;

/**
 * A basic service.
 */
public class ServiceTest extends AbstractService
{
    public int serviceMain(String[] args) throws ServiceException {
        int count = 0;
        while (!shutdown) {
            try {
                Thread.sleep(6000);
            } catch (InterruptedException e) {
            }

            if (++count % 10 == 0)
                EventLog.report("WinRun4J Test Service", EventLog.INFORMATION, "Ping");
        }

        return 0;
    }
}

The serviceMain method is invoked when your service is started. 启动服务时将调用serviceMain方法。 You should not return from this method until your service is ready to shutdown. 在服务准备好关闭之前,您不应该从此方法返回。 Also check the "shutdown" flag - this will be set to true when you click on Stop in the service control panel (or when your service needs to be stopped). 还要检查“shutdown”标志 - 当您在服务控制面板中单击“停止”(或需要停止服务时)时,此标志将设置为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM