将Spring Boot应用程序作为守护服务?

5
我是一名初学者,正在学习Spring Boot。我感觉它是一个非常有用的工具,可以轻松开发Java应用程序。
另一方面,我正在考虑开发一个守护服务,通过Kafka Consumer API从Apache Kafka收集数据/消息,并对检索到的数据进行一些处理。当然,所有这些过程都是定期完成的。
因此,我使用Apache Commons Daemon将应用程序开发为Daemon。但现在,我想使用Spring Boot代替它。
是否可能使用Spring Boot实现这样的服务应用程序?如果可能,请告诉我如何实现。提前致谢!
1个回答

3

我在某处找到了这个内容,对原作者表示歉意。但是我创建了一个项目,并添加了spring-boot-loader依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-loader</artifactId>
    <scope>provided</scope>
</dependency>

由于需要扩展JarLauncher类,Spring Boot提供了一个特殊的启动器,可以改变Java行为类加载器。类org.springframework.boot.loader.JarLauncher创建了一个特殊的类加载器,并引导应用程序。
因为我想将应用程序作为窗口服务启动,所以选择了Procrun作为服务管理器。 Procrun需要两个启动和停止方法或一个具有字符串数组参数的方法(请参见procrun项目以获取更多详细信息)。因此,我创建了一个Bootsrap类,扩展了JarLauncher并实现了Procrun所需的方法。
public class Bootstrap extends JarLauncher {

private static ClassLoader classLoader = null;
private static Bootstrap bootstrap = null;

protected void launch(String[] args, String mainClass, ClassLoader classLoader, boolean wait)
        throws Exception {
    Runnable runner = createMainMethodRunner(mainClass, args, classLoader);
    Thread runnerThread = new Thread(runner);
    runnerThread.setContextClassLoader(classLoader);
    runnerThread.setName(Thread.currentThread().getName());
    runnerThread.start();
    if (wait == true) {
        runnerThread.join();
    }
}

public static void start (String []args) {
    bootstrap = new Bootstrap ();
    try {
        JarFile.registerUrlProtocolHandler();
        classLoader = bootstrap.createClassLoader(bootstrap.getClassPathArchives());
        bootstrap.launch(args, bootstrap.getMainClass(), classLoader, true);
    }
    catch (Exception ex) {
        ex.printStackTrace();
        System.exit(1);
    }
}

public static void stop (String []args) {
    try {
        if (bootstrap != null) {
            bootstrap.launch(args, bootstrap.getMainClass(), classLoader, true);
            bootstrap = null;
            classLoader = null;
        }
    }
    catch (Exception ex) {
        ex.printStackTrace();
        System.exit(1);
    }
}

public static void main(String[] args) {
    String mode = args != null && args.length > 0 ? args[0] : null;
    if ("start".equals(mode)) {
        Bootstrap.start(args);
    }
    else if ("stop".equals(mode)) {
        Bootstrap.stop(args);
    }
}
}

在Spring Boot应用程序类中,我使用以下方法更改了主方法:
private static ApplicationContext applicationContext = null;

public static void main(String[] args) {
    String mode = args != null && args.length > 0 ? args[0] : null;

    if (logger.isDebugEnabled()) {
        logger.debug("PID:" + ManagementFactory.getRuntimeMXBean().getName() + 
                     " Application mode:" + mode + " context:" + applicationContext);
    }
    if (applicationContext != null && mode != null && "stop".equals(mode)) {
        System.exit(SpringApplication.exit(applicationContext, new ExitCodeGenerator() {
            @Override
            public int getExitCode() {
                return 0;
            }
        }));
    }
    else {
        SpringApplication app = new SpringApplication(TestProcrunApplication.class);
        applicationContext = app.run(args);
        if (logger.isDebugEnabled()) {
            logger.debug("PID:" + ManagementFactory.getRuntimeMXBean().getName() + 
                         " Application started context:" + applicationContext);
        }
    }
}

然后,我使用prunsrv.exe安装了该服务:

prunsrv.exe //IS//test-procrun --DisplayName="test-procrun" --Description="test-procrun" --Startup=auto --Install=%CD%\prunsrv.exe --Jvm=auto --Classpath=%CD%..\target\test-procrun-0.0.1-SNAPSHOT.jar --StartMode=jvm --StartClass=it.test.procrun.Bootstrap --StartMethod=start --StartParams=start --StopMode=jvm --StopClass=it.test.procrun.Bootstrap --StopMethod=stop --StopParams=stop --StdOutput=auto --StdError=auto --LogPath=%CD% --LogLevel=Debug

注意:此处为直译,可能需要根据上下文进行调整以使句子更通顺易懂。

非常感谢您的帮助!顺便说一下,很抱歉我忘了提到我正在寻找Linux环境下的守护进程服务,特别是CentOS 7。您能给我一些建议吗? - BG. KIM
你从这里获取了这个:http://zazos79.blogspot.com/2015/02/spring-boot-12-run-as-windows-service.html,希望对你有帮助。-adym - lincolnadym
https://grokonez.com/java-integration/create-windows-service-spring-boot-application-procrun - Prashanth Shyamprasad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接