I have the java application which starts external process through process builder.
External application interracts with rest "world" through stdin
,stdout
,stderr
. Also this process should not be executed longer than some timeout.
code looks like this:
ProcessBuilder pb = new ProcessBuilder(parameters);
Process process = pb.start();
OutputStream processOutputStream = process.getOutputStream();
IOUtils.write(inputJson, processOutputStream); // write data to external process
processOutputStream.close(); we don't need pass more arguments
InputStream errorStream = process.getErrorStream();
boolean responseWithinTimeout = process.waitFor(2000, TimeUnit.MILLISECONDS); //app should work not longer than 2 sec
if (process.isAlive()) {
process.destroyForcibly();
}
String stringFromErrorStream = IOUtils.toString(errorStream, "UTF-8"); //read from external application error stream
My questions:
- Does 2000 niliseconds starts since
pb.start()
or sinceprocess.waitFor
- Is it correct to read from
errorStream
when application already killed or it should be placed beforeprocess.destroyForcibly()
?