从运行时进程读取输出时的BufferedReader循环

时间:2018-12-05 20:12:32

标签: java

在尝试修复流程处理并从流程中获得正确的“是否正常工作”响应时,我引入了一个错误。我从这里的另一篇文章中提取了有关使用缓冲读取器读取Error和Input流的信息,当然,我无法立即找到它。

执行该部分代码后,该代码将陷入循环,并在日志中重复“ ERROR iMCAlarmParse.AlarmToOVO:null ^ M”。我希望在没有错误的情况下,readLine不会读取“ null”,而是告诉要进行的时间,但是目前这是一个任性的儿子。

编辑:从CLI运行opcmsg命令时,它可以正常工作,只是返回一个新提示。没有任何表示成功的信息,但如果出错,则会显示一些模糊的错误。

非常感谢。

            Runtime rt = Runtime.getRuntime();
            // Construct the command line 
            String msgCmd = "opcmsg ...";  //Truncated as it really dosn't matter what the CLI optoins are.
            log4j.debug(msgCmd);

            // Execute the CLI call to opcmsg
            Process pr = rt.exec(msgCmd);

            BufferedReader brErr = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
            BufferedReader brOut = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String strErrLine;
            String strOutLine;

            while ((strErrLine = brErr.readLine()) != "") {
      // This is where the error noted above gets stuck
                log4j.error(strErrLine);
            }
            while ((strOutLine = brOut.readLine()) != "") {
                log4j.debug(strOutLine);
            }
            if(pr.waitFor(5, TimeUnit.SECONDS)) {
                if (pr.exitValue() != 0) {
                    log4j.error("Abnormal opcmsg termination for alarm ID: " + mAlarm.get("id"));
                    boolCmdResult = false;
                    return;
                } else {
                    log4j.debug("Normal opcmsg execution for alarm ID: " + mAlarm.get("id"));
                    boolCmdResult = true;
                    return;
                }
            } else {
                pr.destroy();
                log4j.error("opcmsg failed to respond within 5 seconds executing alarm ID: " + mAlarm.get("id"));
            }

0 个答案:

没有答案
相关问题