SpringBoot实现调用自定义的应用程序((最新推荐)
更新时间:2024年06月21日 15:18:04 作者:宋宋大王
这篇文章主要介绍了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));到此这篇关于SpringBoot实现调用自定义的应用程序的文章就介绍到这了,更多相关SpringBoot应用程序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
一篇文章带你搞定 springsecurity基于数据库的认证(springsecurity整合mybatis)
这篇文章主要介绍了一篇文章带你搞定 springsecurity基于数据库的认证(springsecurity整合mybatis),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-10-10
IntelliJ IDEA 2022.1.1 沒有CVS的过程分析
这篇文章主要介绍了IntelliJ IDEA 2022.1.1 沒有CVS的过程解析,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-07-07
MybatisPlus3.5.5与pagehelper starter2.1.0冲突的问题解决
在使用MybatisPlus 3.5.5与PageHelper Starter 2.1.0时,由于引用了不同版本的jsqlparser库(4.6与4.7),会导致运行时错误,解决方案涉及确认依赖版本,本文就来介绍一下,感兴趣的同学可以下载学习2024-10-10


最新评论