search

Home  >  Q&A  >  body text

怎么让一个java程序接收命令后重启

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

高洛峰高洛峰2889 days ago511

reply all(3)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 09:31:42

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

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:31:42

    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);
                    }
                }
            });
    
        }

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:31:42

    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

    reply
    0
  • Cancelreply