SpringBoot实现调用自定义的应用程序

1.应用程序设置全局可执行

添加安装路径到全局变量中,并执行source指令使其生效

export PATH=$PATH:/the/path/to/software
source /etc/profile

2.在代码中配置调用程序的指令,并在Service中引入

coverage:
  command: coverage
@Value("${coverage.command}")
private String coverageCommand;

3.编写命令执行方法

    /*
     *调用命令并将执行日志写入文件中
     */
public void exeCmd(String commandStr, String logFile) {
        BufferedReader br = null;
        String line = null;
        StringBuilder stringBuild = new StringBuilder();
        try {
            Process p = Runtime.getRuntime().exec(commandStr);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = br.readLine()) != null) {
                stringBuild.append(line + "\n");
                log.info(line);
                try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new String(logFile.getBytes("utf-8"))), "utf-8")) {
                    out.append(stringBuild);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

     /*
     *调用命令并返回全部命令执行日志
     */
    public String getVariable(String command) throws IOException {
        BufferedReader br = null;
        String line = null;
        List<String> strings = new ArrayList<>();
        StringBuilder stringBuild = new StringBuilder();
        try {
            Process p = Runtime.getRuntime().exec(command);
            br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = br.readLine()) != null) {
                stringBuild.append(line + "\n");
                strings.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return strings.toString();
    }

4.如命令执行时间过长,可先返回命令调用情况,后续进行任务的更新操作

ExecutorService executorService = Executors.newFixedThreadPool(2);
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
                @Override
                public Integer get() {
                    log.info("开始执行算法-------");
                    exeCmd(commendStr, outLog());
                    log.info("算法执行结束");
                    File txtFile = new File(outLog);
                    //根据实际加工逻辑进行更新或其他操作
                    if (txtFile.exists()) {
                        task.setSuccessTime(new Date());
                        task.setTaskStatus("SUCCESS");
                    } else {
                        task.setErrorTime(new Date());
                        task.setTaskStatus("ERROR");
                    }
                    taskMapper.updateTaskInfo(task);
                    return 3;
                }
            }, executorService);
future.thenAccept(e -> System.out.println(e));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值