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应用程序内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论