SpringBoot實(shí)現(xiàn)調(diào)用自定義的應(yīng)用程序((最新推薦)
1.應(yīng)用程序設(shè)置全局可執(zhí)行
添加安裝路徑到全局變量中,并執(zhí)行source指令使其生效
export PATH=$PATH:/the/path/to/software
source /etc/profile
2.在代碼中配置調(diào)用程序的指令,并在Service中引入
coverage: command: coverage
@Value("${coverage.command}")
private String coverageCommand;3.編寫(xiě)命令執(zhí)行方法
/*
*調(diào)用命令并將執(zhí)行日志寫(xiě)入文件中
*/
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();
}
}
}
}
/*
*調(diào)用命令并返回全部命令執(zhí)行日志
*/
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.如命令執(zhí)行時(shí)間過(guò)長(zhǎng),可先返回命令調(diào)用情況,后續(xù)進(jìn)行任務(wù)的更新操作
ExecutorService executorService = Executors.newFixedThreadPool(2);
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(new Supplier<Integer>() {
@Override
public Integer get() {
log.info("開(kāi)始執(zhí)行算法-------");
exeCmd(commendStr, outLog());
log.info("算法執(zhí)行結(jié)束");
File txtFile = new File(outLog);
//根據(jù)實(shí)際加工邏輯進(jìn)行更新或其他操作
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));到此這篇關(guān)于SpringBoot實(shí)現(xiàn)調(diào)用自定義的應(yīng)用程序的文章就介紹到這了,更多相關(guān)SpringBoot應(yīng)用程序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類(lèi)
這篇文章主要介紹了Java實(shí)現(xiàn)的properties文件動(dòng)態(tài)修改并自動(dòng)保存工具類(lèi),可實(shí)現(xiàn)針對(duì)properties配置文件的相關(guān)修改與保存功能,需要的朋友可以參考下2017-11-11
實(shí)例分析java中重載與重寫(xiě)的區(qū)別
這篇文章主要介紹了實(shí)例分析java中重載與重寫(xiě)的區(qū)別,需要的朋友可以參考下2014-07-07
關(guān)于Java Interface接口的簡(jiǎn)單練習(xí)題
這篇文章主要給大家分享的是關(guān)于Java Interface接口的簡(jiǎn)單練習(xí)題,難度不算大,但是要有一個(gè)清晰的邏輯建立接口和鏈接Java類(lèi)。下面來(lái)看看文章的詳細(xì)介紹吧,需要的朋友可以參考一下2021-11-11
IO密集型任務(wù)設(shè)置線程池線程數(shù)實(shí)現(xiàn)方式
這篇文章主要介紹了IO密集型任務(wù)設(shè)置線程池線程數(shù)實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
Java調(diào)用groovy實(shí)現(xiàn)原理代碼實(shí)例
這篇文章主要介紹了Java調(diào)用groovy實(shí)現(xiàn)原理代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Java實(shí)現(xiàn)PDF導(dǎo)出功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)PDF導(dǎo)出功能的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-09-09
使用spring boot 整合kafka,延遲啟動(dòng)消費(fèi)者
這篇文章主要介紹了使用spring boot 整合kafka,延遲啟動(dòng)消費(fèi)者的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

