2016-03-17 93 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

相關問題