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.編寫命令執(zhí)行方法
/* *調(diào)用命令并將執(zhí)行日志寫入文件中 */ 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í)間過長(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("開始執(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)文章
SpringBoot實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)釘釘機(jī)器人消息推送的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Android?Studio中創(chuàng)建java工程的完整步驟
Android?Studio創(chuàng)建java工程是非常麻煩的,因?yàn)锳ndroid?Studio沒有提供直接創(chuàng)建java工程的方法,下面這篇文章主要給大家介紹了關(guān)于Android?Studio中創(chuàng)建java工程的完整步驟,需要的朋友可以參考下2024-01-01一篇文章帶你搞定 springsecurity基于數(shù)據(jù)庫的認(rèn)證(springsecurity整合mybatis)
這篇文章主要介紹了一篇文章帶你搞定 springsecurity基于數(shù)據(jù)庫的認(rèn)證(springsecurity整合mybatis),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10IntelliJ?IDEA?2022.1.1?沒有CVS的過程分析
這篇文章主要介紹了IntelliJ?IDEA?2022.1.1?沒有CVS的過程解析,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07MybatisPlus3.5.5與pagehelper?starter2.1.0沖突的問題解決
在使用MybatisPlus?3.5.5與PageHelper?Starter?2.1.0時(shí),由于引用了不同版本的jsqlparser庫(4.6與4.7),會(huì)導(dǎo)致運(yùn)行時(shí)錯(cuò)誤,解決方案涉及確認(rèn)依賴版本,本文就來介紹一下,感興趣的同學(xué)可以下載學(xué)習(xí)2024-10-10SpringBoot使用jsr303校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot使用jsr303校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10