2016-03-17 162 views
2

我是一个刚开始学习Spring Boot的新手。我觉得这对于轻松开发Java应用程序来说是一个非常有用且非常好的工具。另一方面,我正在考虑开发一个守护程序服务,它通过Kafka Consumer API从Apache Kafka收集数据/消息并执行一些关于检索数据的过程。这整个过程当然是定期完成的。作为守护程序服务的Spring Boot应用程序?

所以我一直在使用Apache Commons Daemon开发应用程序作为守护进程。然而,我现在想用Spring Boot代替它。

是否可以通过Spring Boot实现这样的服务应用程序?如果可能的话,请让我知道它是如何实施的。提前致谢!

回答

2

我发现这个地方所以道歉,原来的主人,但我要创造我加弹簧引导装载程序依赖

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

因为需要延长JarLauncher类的项目。 Spring引导提供了一个特殊的启动器,它改变了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); 
    } 
} 
} 

在弹簧引导应用I类与改变的主要方法:

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 =调试

+0

谢谢大家的帮助!顺便说一句,对不起,我忘记提及我正在Linux环境中寻找守护进程服务,特别是CentOS 7.你能给我任何建议吗? –

+0

认为你得到这个来自:http://zazos79.blogspot.com/2015/02/spring-boot-12-run-as-windows-service.html,hth -adym – lincolnadym

相关问题