示例#1
0
  /**
   * Runs the process builded by the given process builder.
   *
   * @param processBuilder the builder of the process
   * @return the process execution result
   * @throws Exception if a problem occurs amidst execution of the process
   */
  public static ExecutionResult runProcess(ProcessBuilder processBuilder) throws Exception {

    final Process startedProcess = processBuilder.start();

    Callable<List<String>> stdOutCallable = createStreamCallable(startedProcess.getInputStream());
    Callable<List<String>> stdErrCallable = createStreamCallable(startedProcess.getErrorStream());
    Callable<Integer> waitForCallable = createWaitForCallable(startedProcess);

    ExecutorService executor = Executors.newFixedThreadPool(3);
    Future<List<String>> stdOutFuture = executor.submit(stdOutCallable);
    Future<List<String>> stdErrFuture = executor.submit(stdErrCallable);
    Future<Integer> waitForFuture = executor.submit(waitForCallable);

    ExecutionResult executionResult = new ExecutionResult();

    executionResult.setReturnValue(getWaitForCallableResult(waitForFuture));
    executionResult.setStdOut(getStreamCallableResult(stdOutFuture));
    executionResult.setStdErr(getStreamCallableResult(stdErrFuture));

    executor.shutdownNow();

    return executionResult;
  }