当前位置: 代码网 > it编程>编程语言>Java > SpringBoot实现调用自定义的应用程序((最新推荐)

SpringBoot实现调用自定义的应用程序((最新推荐)

2024年07月03日 Java 我要评论
1.应用程序设置全局可执行添加安装路径到全局变量中,并执行source指令使其生效export path=$path:/the/path/to/softwaresource /etc/profile2

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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com