2017-10-23 19 views
1

私たちはinstall4jバージョン6.1.5を使用しており、Windowsサービスとしてglassfishをインストールする必要があります。したがって、私はドキュメントを読んで、Windowsサービスラッパークラスを実装しようとしました。これは、サービスアクションのためにこれを使用するインストールのためのサービスランチャーを作成することができます。これはコードです:install4jでサービスとしてglassfishをインストールするための推奨される方法はありますか?

public class WindowsServiceWrapper implements Runnable { 
    private static volatile boolean keepRunning = true; 

    public static void main(String[] args) { 
     final Thread mainThread = Thread.currentThread(); 

     String glassfishInstallDir = System.getProperty("installation.dir"); 
     if (glassfishInstallDir == null || glassfishInstallDir.isEmpty()) { 
      System.err.println("Path not set to glassfish installation but required for startup the service."); 
      System.exit(1); 
     } else if (!new File(glassfishInstallDir).exists()) { 
      System.err.println("Path '" + glassfishInstallDir + "' do not exists. Startup of the service aborted."); 
      System.exit(1); 
     } 

     System.out.println("Starting the service."); 

     String asadminPath = glassfishInstallDir + "/bin/asadmin.bat"; 

     CommandLine commandLine = CommandLine.parse(asadminPath + " start-domain"); 
     runCommand(commandLine); 

     // register to jvm shutdown to handle service shutdown 
     Runtime.getRuntime().addShutdownHook(new Thread() { 
      public void run() { 
       keepRunning = false; 
       try { 
        System.out.println("Shutdown of the service was triggered."); 
        mainThread.join(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

     new WindowsServiceWrapper(asadminPath).run(); 
    } 

    private final String ASADMIN_PATH; 

    private WindowsServiceWrapper(String asadminPath) { 
     ASADMIN_PATH = asadminPath; 
    } 

    public void run() { 
     while (keepRunning) { 
      // nothing to do here but waiting for shutdown hook 
     } 

     System.out.println("Shutdown the service."); 

     CommandLine commandLine = CommandLine.parse(ASADMIN_PATH + " stop-domain"); 
     runCommand(commandLine); 
    } 

    private static int runCommand(CommandLine cmdLine) { 
     DefaultExecutor executor = new DefaultExecutor(); 
     int exitCode; 
     try { 
      exitCode = executor.execute(cmdLine); 
     } catch (IOException e) { 
      System.out.println("Error occured during command execution: " + cmdLine.toString()); 
      e.printStackTrace(); 
      exitCode = 1; 
      keepRunning = false; 
     } 
     return exitCode; 
    } 
} 

私は、Apacheからcommons-execライブラリを使用するのGlassFishサーバーを起動および停止するには。このコードは、サービスのインストールと起動、およびサービスのアンインストールに適しています。残っているのは1つだけです。サービスの起動時に、グラスフィッシュの起動が完全に完了し、コモンズ・エグゼクティブ・コールが完了するまで待つ必要があります。依存インストール手順を進める必要があります。今のところ、「サービスの開始」アクションは即座に戻ります。私のラッパークラスの主なメソッドは、未熟なスレッドとしてインスタンス化されていると思いますか?このようなことをどのように処理するのが良い方法がありますか?

答えて

0

「HTTPサーバーの待機」アクションを使用して、サーバーが始動するまで待つことができます。

関連する問題