1、
2、
| import org.jeecg.zhongyi.auto_dep.util.CommandStreamGobbler; |
| import org.jeecg.zhongyi.util.LogbackUtil; |
| import org.jeecg.zhongyi.util.vo.Result; |
| |
| import java.io.IOException; |
| import java.io.InputStreamReader; |
| import java.util.LinkedList; |
| import java.util.List; |
| import java.util.concurrent.TimeUnit; |
| |
| public class auto_dep_helper { |
| public static Result dep_env_wait(String cmd_sh) { |
| Process process = null; |
| List<String> commandList = new LinkedList<>(); |
| |
| commandList.add(cmd_sh); |
| commandList.add("hello world "); |
| String[] commands = new String[commandList.size()]; |
| for (int i = 0; i < commandList.size(); i++) { |
| commands[i] = commandList.get(i); |
| } |
| InputStreamReader stdISR = null; |
| InputStreamReader errISR = null; |
| |
| String logName = cmd_sh; |
| try { |
| process = Runtime.getRuntime().exec(commands); |
| |
| CommandStreamGobbler errorGobbler = new CommandStreamGobbler(process.getErrorStream(), logName, "ERR"); |
| CommandStreamGobbler outputGobbler = new CommandStreamGobbler(process.getInputStream(), logName, "STD"); |
| |
| errorGobbler.start(); |
| |
| while (!errorGobbler.isReady()) { |
| Thread.sleep(10); |
| } |
| outputGobbler.start(); |
| while (!outputGobbler.isReady()) { |
| Thread.sleep(10); |
| } |
| |
| int exitValue = process.waitFor(); |
| System.out.println("exitValue=" + exitValue); |
| if (exitValue == 0) { |
| LogbackUtil.getErrorLogger().info("更新部署成功"); |
| return Result.success(); |
| } else { |
| LogbackUtil.getErrorLogger().info("更新部署失败"); |
| return Result.error(); |
| } |
| } catch (IOException | InterruptedException e) { |
| e.printStackTrace(); |
| LogbackUtil.getErrorLogger().error(e.getMessage(), e); |
| return Result.error(e.getMessage()); |
| } finally { |
| try { |
| if (stdISR != null) { |
| stdISR.close(); |
| } |
| if (errISR != null) { |
| errISR.close(); |
| } |
| if (process != null) { |
| process.destroy(); |
| } |
| } catch (IOException e) { |
| System.out.println("正式执行命令:" + logName + "有IO异常"); |
| LogbackUtil.getErrorLogger().error(e.getMessage(), e); |
| return Result.error(e.getMessage()); |
| } |
| } |
| } |
| |
| public static Result dep_env_not_wait(String cmd_sh) { |
| Process process = null; |
| List<String> commandList = new LinkedList<>(); |
| |
| commandList.add(cmd_sh); |
| commandList.add("hello world "); |
| String[] commands = new String[commandList.size()]; |
| for (int i = 0; i < commandList.size(); i++) { |
| commands[i] = commandList.get(i); |
| } |
| InputStreamReader stdISR = null; |
| InputStreamReader errISR = null; |
| |
| String logName = cmd_sh; |
| try { |
| process = Runtime.getRuntime().exec(commands); |
| |
| CommandStreamGobbler errorGobbler = new CommandStreamGobbler(process.getErrorStream(), logName, "ERR"); |
| CommandStreamGobbler outputGobbler = new CommandStreamGobbler(process.getInputStream(), logName, "STD"); |
| |
| errorGobbler.start(); |
| |
| while (!errorGobbler.isReady()) { |
| Thread.sleep(10); |
| } |
| outputGobbler.start(); |
| while (!outputGobbler.isReady()) { |
| Thread.sleep(10); |
| } |
| |
| boolean exitValue = process.waitFor(5, TimeUnit.SECONDS); |
| System.out.println("exitValue=" + exitValue); |
| if (exitValue) { |
| LogbackUtil.getErrorLogger().info("更新部署成功,请稍等,并验证结果"); |
| return Result.success("更新部署成功,请稍等,并验证结果"); |
| } else { |
| LogbackUtil.getErrorLogger().info("更新部署失败,请稍等,并验证结果"); |
| return Result.error("更新部署超时,请稍等,并验证结果"); |
| } |
| } catch (IOException | InterruptedException e) { |
| e.printStackTrace(); |
| LogbackUtil.getErrorLogger().error(e.getMessage(), e); |
| return Result.error(e.getMessage()); |
| } finally { |
| try { |
| if (stdISR != null) { |
| stdISR.close(); |
| } |
| if (errISR != null) { |
| errISR.close(); |
| } |
| if (process != null) { |
| process.destroy(); |
| } |
| } catch (IOException e) { |
| System.out.println("正式执行命令:" + logName + "有IO异常"); |
| LogbackUtil.getErrorLogger().error(e.getMessage(), e); |
| return Result.error(e.getMessage()); |
| } |
| } |
| } |
| } |
| import java.io.BufferedReader; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.io.InputStreamReader; |
| import java.util.LinkedList; |
| import java.util.List; |
| |
| public class CommandStreamGobbler extends Thread { |
| |
| private InputStream is; |
| |
| private String command; |
| |
| private String prefix = ""; |
| |
| private boolean readFinish = false; |
| |
| private boolean ready = false; |
| |
| private List<String> infoList = new LinkedList<String>(); |
| |
| public CommandStreamGobbler(InputStream is, String command, String prefix) { |
| this.is = is; |
| this.command = command; |
| this.prefix = prefix; |
| } |
| |
| public void run() { |
| InputStreamReader isr = null; |
| try { |
| isr = new InputStreamReader(is); |
| BufferedReader br = new BufferedReader(isr); |
| String line = null; |
| ready = true; |
| |
| while (br != null && (line = br.readLine()) != null) { |
| infoList.add(line); |
| System.out.println(prefix + " line: " + line); |
| LogbackUtil.getErrorLogger().info(prefix + " line: " + line); |
| } |
| } catch (IOException ioe) { |
| System.out.println("正式执行命令:" + command + "有IO异常"); |
| LogbackUtil.getErrorLogger().error("正式执行命令:" + command + "有IO异常", ioe); |
| } finally { |
| try { |
| if (isr != null) { |
| isr.close(); |
| } |
| } catch (IOException ioe) { |
| System.out.println("正式执行命令:" + command + "有IO异常"); |
| LogbackUtil.getErrorLogger().error("正式执行命令:" + command + "有IO异常", ioe); |
| } |
| readFinish = true; |
| } |
| } |
| |
| public InputStream getIs() { |
| return is; |
| } |
| |
| public String getCommand() { |
| return command; |
| } |
| |
| public boolean isReadFinish() { |
| return readFinish; |
| } |
| |
| public boolean isReady() { |
| return ready; |
| } |
| |
| public List<String> getInfoList() { |
| return infoList; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2020-06-18 【Redis__验证】手机验证和限制登录功能
2019-06-18 【Js 文件】 相关
2019-06-18 【mui】