怎么让一个java程序接收命令后重启
高洛峰
高洛峰 2017-04-18 09:29:42
0
3
504

1 一个web程序部署在tomcat程序中,
怎么让程序接受重启命令后重启

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
Peter_Zhu

You can make an independent interface service and execute a start and stop script, so that you can achieve the effect you want

小葫芦

Directly use java to call the system command to restart tomcat
The code is probably like this:

public static void runShell(final String cmd) throws IOException, InterruptedException {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Runtime run = Runtime.getRuntime();
                    Process p = run.exec(cmd);
                    BufferedInputStream in = new BufferedInputStream(p.getInputStream());
                    BufferedReader inBr = new BufferedReader(new InputStreamReader(in));
                    String lineStr;
                    while ((lineStr = inBr.readLine()) != null) {
                        logger.info(lineStr);// 打印输出信息
                    }

                    //检查命令是否执行失败。
                    if (p.waitFor() != 0) {
                        if (p.exitValue() == 1) {// 0表示正常结束,1:非正常结束
                            logger.error(cmd + "命令执行失败!");
                        }
                    }
                    inBr.close();
                    in.close();
                } catch (Exception e) {
                    logger.error("调用系统命令失败!", e);
                }
            }
        });

    }
左手右手慢动作

Restarting the web program is nothing more than ending the process, executing the system exit 4 function, and then executing the main method to start the container. This program must be independent

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template