系統重啓-------即java代碼重啓tomcat!

    /**
     * 重啓RA系統
     *
     * @return
     * @author wly
     */
    @RequestMapping(value = "/v1/init/ra/restart", method = RequestMethod.GET)
    public Object restartRaServer(HttpServletResponse resp) {
        logger.info("InitController.initDeviceRecover>>>>>>start");

        Result result = new Result();
        //tomcat路徑 /usr/local/tomcat
        //String tomcatPath = File.separator + "usr" + File.separator + "local" + File.separator + "tomcat";
        //本地測試
        String tomcatPath = "E:\\tools\\apache-tomcat-8.5.39";
        logger.info(tomcatPath);

        // 調用密碼機
        boolean restartTomcat = GMSSLTomcatUtils.restartTomcat(tomcatPath);
        if (!restartTomcat) {
            logger.error("重啓tomcat失敗");
            result.setError(ErrorEnum.RESTART_TOMCAT_ERROR);
            return result;
        }
        
        logger.info("InitController.restartRaServer>>>>>>result:" + JsonUtils.object2Json(result));
        return result;
    }

 

 public class GMSSLTomcatUtils {
    private static Logger logger = LoggerFactory.getLogger(GMSSLTomcatUtils.class);

    public static boolean restartTomcat(String tomcatPath) {
        if (!shutdownTomcat(tomcatPath)) {
            return false;
        }
        return startupTomcat(tomcatPath);
    }
    public static boolean shutdownTomcat(String tomcatPath) {
        return run(tomcatPath, "shutdown");
    }

    public static boolean startupTomcat(String tomcatPath) {
        return run(tomcatPath, "startup");
    }
    private static void run(String tomcatPath, String shName) {
        Runtime rt = Runtime.getRuntime();
        Process ps = null;

        try {
            String os = System.getProperty("os.name");
            if (os.startsWith("Windows")) {
                ps = rt.exec("cmd /c " + tomcatPath + File.separator + "bin" + File.separator + shName + ".bat", (String[])null, new File(tomcatPath));
            } else {
                ps = rt.exec("sh " + tomcatPath + File.separator + "bin" + File.separator + shName + ".sh", (String[])null, new File(tomcatPath));
            }

            InputStream is = ps.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            String line;
            while((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            ps.waitFor();
            is.close();
            reader.close();
            ps.destroy();
        } catch (Exception var8) {
            var8.printStackTrace();
            return false;
            
        }

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章