2017-05-31 9 views
-1

私はinstall4jでアプリケーションを作成しましたが、インストールが成功した後、Linux/Windowsマシンでサービスを再起動する必要があります。install4jで「サービスの開始/停止」を構成する方法は?

javaでサービスを停止して開始するために、次のスクリプトを書いています。しかし、私はこの操作をinstall4j内で処理したいと思います。

int restartService = Util.showOptionDialog("\nClick on \"Yes\" to restart the service now and Click on \"No\" to restart the service manually later.",new String[]{"Yes","No"},1); 
//0= yes restart 
//1= no 

if(restartService == 0){ 
    if(Util.isWindows()) 
    { 
     context.getProgressInterface().setStatusMessage("Checking service is present or not..."); 
     context.getProgressInterface().setPercentCompleted(10); 
     String serviceName = "vspherewebclientsvc"; 
      try { 
       boolean isProcessActive = false; 
       Util.logInfo(null,"Step 1 : Checking service is present or not..."); 
       Process p = Runtime.getRuntime().exec("sc query " + serviceName); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 
       String line = reader.readLine(); 
       while (line != null) { 
        if (line.trim().startsWith("STATE")) { 
         while (!("4".equals(line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim()) || "2".equals(line.trim().substring(line.trim().indexOf(":") + 1, line.trim().indexOf(":") + 4).trim()))) { 
          Util.logInfo(null,"running"); 
         } 
         Util.logInfo(null,"\t-> Service is Active!"); 
         isProcessActive = true; 
        } 
        line = reader.readLine(); 
       } 

       if (isProcessActive) { 
        context.getProgressInterface().setStatusMessage("Stopping service..."); 
        context.getProgressInterface().setPercentCompleted(20); 
        Util.logInfo(null,"Step 2 : Stopping service..."); 
        String[] stop = {"cmd.exe", "/c", "net", "stop", serviceName, "/yes"}; 
        Process p1 = Runtime.getRuntime().exec(stop); 
        p1.waitFor(); 
        BufferedReader reader1 = new BufferedReader(new InputStreamReader(p1.getInputStream())); 
        String line1 = reader1.readLine(); 
        while (line1 != null) { 
         line1 = reader1.readLine(); 
        } 
        context.getProgressInterface().setStatusMessage("Waiting for Stop service..."); 
        context.getProgressInterface().setPercentCompleted(40); 
        Thread.sleep(30000); 

        context.getProgressInterface().setStatusMessage("Validating stop operation..."); 
        context.getProgressInterface().setPercentCompleted(50); 
        boolean isProcessStopped = false; 
        Process p2 = Runtime.getRuntime().exec("sc query " + serviceName); 
        BufferedReader reader2 = new BufferedReader(new InputStreamReader(p2.getInputStream())); 
        String line2 = reader2.readLine(); 
        while (line2 != null) { 
         if (line2.trim().startsWith("STATE")) { 
          while (!("1".equals(line2.trim().substring(line2.trim().indexOf(":") + 1, line2.trim().indexOf(":") + 4).trim()) || "3".equals(line2.trim().substring(line2.trim().indexOf(":") + 1, line2.trim().indexOf(":") + 4).trim()))) { 
           Util.logInfo(null,"stopping"); 
          } 
          Util.logInfo(null,"\t-> Finished with Stop operation!"); 
          isProcessStopped = true; 
         } 
         line2 = reader2.readLine(); 
        } 

        context.getProgressInterface().setStatusMessage("Starting service..."); 
        context.getProgressInterface().setPercentCompleted(60); 
        if (isProcessStopped) { 
         Util.logInfo(null,"Step 3 : Starting service..."); 
         String[] start = {"cmd.exe", "/c", "sc", "start", serviceName}; 
         Process p3 = Runtime.getRuntime().exec(start); 
         p3.waitFor(); 
         BufferedReader reader3 = new BufferedReader(new InputStreamReader(p3.getInputStream())); 
         String line3 = reader3.readLine(); 
         while (line3 != null) { 
          line3 = reader3.readLine(); 
         } 
         Util.logInfo(null,"\t-> Finished with Start operation!"); 
        } 
       } else { 
        Util.logInfo(null,"Error : Service is not Active..."); 
       } 
       context.getProgressInterface().setStatusMessage("Validating start operation..."); 
       context.getProgressInterface().setPercentCompleted(80); 
       Thread.sleep(30000); 

       Util.logInfo(null,"Step 4 : Validating Restart operation!"); 
       boolean isProcessRestarted = false; 
       Process p4 = Runtime.getRuntime().exec("sc query " + serviceName); 
       BufferedReader reader4 = new BufferedReader(new InputStreamReader(p4.getInputStream())); 
       String line4 = reader4.readLine(); 
       while (line4 != null) { 
        if (line4.trim().startsWith("STATE")) { 
         while (!("4".equals(line4.trim().substring(line4.trim().indexOf(":") + 1, line4.trim().indexOf(":") + 4).trim()) || "2".equals(line4.trim().substring(line4.trim().indexOf(":") + 1, line4.trim().indexOf(":") + 4).trim()))) { 
          Util.logInfo(null,"restarting"); 
         } 
         isProcessRestarted = true; 
        } 
        line4 = reader4.readLine(); 
       } 

       if (isProcessRestarted) { 
        Util.logInfo(null,"\t-> Restart operation is successfull!"); 
        context.getProgressInterface().setStatusMessage("Restart operation is successfull!"); 
        context.getProgressInterface().setPercentCompleted(90); 
       } else { 
        Util.logInfo(null,"\t-> Restart operation is not successfull!"); 
        context.getProgressInterface().setStatusMessage("Restart operation is not successfull!"); 
        context.getProgressInterface().setPercentCompleted(90); 
       } 
       context.getProgressInterface().setStatusMessage("Completed!"); 
       context.getProgressInterface().setPercentCompleted(100); 
       return true; 
      } catch (InterruptedException ex) { 
       return false; 
      } 
    } 
    else if (Util.isLinux()) 
    { 
    try { 
       String[] command = {"/bin/bash","-c","/etc/init.d/vsphere-client restart"}; 
       Process p = Runtime.getRuntime().exec(command); 
       p.waitFor(); 
       Util.logInfo(null,"Done"); 
       return true; 
      } catch (IOException ex) { 
       return false; 
      } 
    }else{ 
     return false; 
    } 
}else{ 
    context.goBack(0); 
    return false; 
} 

「サービスの開始/停止」でinstall4jを使用するにはどうすればよいですか?

答えて

1

この目的で使用できるinstall4jには、「サービスの停止」アクションと「サービスの開始」アクションがあります。

+0

はい。しかし、私はinstall4jでこれを実装するためのいくつかの例が必要です。もしあなたが私にこのための参照/リンクを教えてくれれば、とても役に立ちます。 –

+0

同じサービスに対して「サービスの停止」と「サービスの開始」アクションを順番に追加するだけで、サービスが再開されます。サービスを持つプロジェクトの例は "hello"サンプルプロジェクトです。 –

関連する問題