用commons-exec流输出? [英] Streaming output with commons-exec?

查看:230
本文介绍了用commons-exec流输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一个例子,说明如何流式传输用DefaultExecutor执行的外部程序的输出?我找不到任何描述该操作方法的文档.

Can anyone give me an example of how to stream the output of an external program executed with DefaultExecutor? I'm not finding any documentation describing how to do this.

我的外部过程将运行几个小时,因此仅获取所有输出数据是不可行的;它必须流式传输.

My external process will run for several hours, so just grabbing all output data isn't feasible; it must be streamed.

推荐答案

注意:此解决方案是同步的,因此不会流式传输.您将需要在单独的线程中进行读取,或者使用execute命令的异步版本.

Note: this solution is synchronous, so it won't stream. You'll need to read is in a separate thread, or use the asynchronous version of the execute command.

private InputStream getStream() {

 String dataParsingCommand = "java";

PipedOutputStream output = new PipedOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(output);

CommandLine cl = CommandLine.parse(command);
cl.addArgument("-jar");
cl.addArgument(dataParserPath);

DefaultExecutor exec = new DefaultExecutor();
DataInputStream is = null;
try {
    is = new DataInputStream(new PipedInputStream(output));
    exec.setStreamHandler(psh);
    exec.execute(dataParserCommandLine);
} catch (ExecuteException ex) {
} catch (IOException ex) {
}

return is;
}

这篇关于用commons-exec流输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆