我一直在阅读文档,但我无法启动和停止服务。
我的 .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
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;
}
}
}
}
}
我必须修改这个类,但我不知道如何修改。
谢谢。
最佳答案
从 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;
}
}
serviceMain 方法在您的服务启动时被调用。在您的服务准备好关闭之前,您不应从此方法返回。还要检查“关闭”标志 - 当您在服务控制面板中单击“停止”时(或者当您的服务需要停止时),这将被设置为真。
关于java - 如何使用 winrun4j 创建 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5759561/