我需要执行一个外部应用程序,该程序返回大量数据(需要超过2个小时才能完成),并且不断输出数据。我的需求是异步执行此程序,并将输出捕获到文件中。我尝试使用Java进程构建器,但它似乎会挂起并仅在程序退出或被强制终止时返回输出。我尝试使用进程构建器并生成一个新线程来捕获输出,但这也没有帮助。然后我了解了Apache Commons Exec,并尝试了相同的方法。但是,这似乎也需要很长时间,并返回不同的错误代码(对于相同的输入)。
任何帮助或工作示例都将非常有帮助。
CommandLine cmdLine = new CommandLine("/opt/testsimulator");
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler(stdout);
ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);
Executor executor = new DefaultExecutor();
executor.setStreamHandler(psh);
executor.setWatchdog(watchdog);
try {
executor.execute(cmdLine);
} catch (ExecuteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
任何帮助或工作示例都将非常有帮助。