如何在install4j中配置“启动/停止服务”?

时间:2017-05-31 09:29:00

标签: install4j

我用install4j创建了一个应用程序,安装成功后,我需要在Linux / Windows机器上重启少量服务。

我编写了一个以下脚本来停止和启动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 个答案:

答案 0 :(得分:2)

在install4j中有“停止服务”和“启动服务”操作,您可以将其用于此目的。