2017-05-31 1 views
-1

J'ai créé une application avec install4j et après une installation réussie, j'ai besoin de redémarrer quelques services sous Linux/Windows.Comment configurer "Démarrer/Arrêter un service" dans install4j?

J'ai écrit un script suivant pour arrêter et démarrer les services dans java. Mais je veux gérer cette opération dans 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; 
} 

Comment pouvons-nous y parvenir avec "Start/Stop un service" à install4j?

Répondre

1

Il existe des actions "Arrêter un service" et "Démarrer un service" dans install4j que vous pouvez utiliser à cette fin.

+0

Oui. Mais j'ai besoin d'un exemple pour implémenter ceci avec install4j. Ce sera très utile pour moi si vous me partagez une référence/un lien pour cela. –

+0

Vous venez d'ajouter une action "Arrêter un service" et "Démarrer un service" en séquence pour le même service, puis le redémarrer. Un exemple de projet avec un service est le projet exemple "Bonjour". –