Apache CommonsExec异步调用阻止同步调用

时间:2013-07-03 15:13:20

标签: java apache maven apache-commons-exec

我有两个使用CommonsExec执行的调用 - 一个是标准同步调用,我在其中调用批处理文件来编译maven项目,下一个是对运行编译命令行的批处理文件的异步调用项目

maven批处理文件看起来像

call mvn package

完成两次,编译并启动两个程序。

第一次工作正常,但第二次同步构建调用由于某种原因没有返回,尽管记录的输出显示构建成功完成。那么这个程序显然没有启动。

我也可以通过运行,然后编译来重新创建它 - 似乎只要异步调用正在运行,同步调用就不会完成。

有人可以帮忙吗?

以上代码是

 private static final String LAUNCH_CLIENT_FORMAT = "\"%s\\start.bat\" http://localhost:%d" + ENDPOINT;
 private static final String COMPILE_FORMAT = "\"%s\\compile.bat\"";

 private static boolean compileAndLaunch(String aiDirectory, int port) {
   System.out.println("Compiling " + aiDirectory + " for port " + port);
   if (!run(String.format(COMPILE_FORMAT, aiDirectory), aiDirectory))
     return false;

   System.out.println("Done compiling " + aiDirectory + " for port " + port + ", launching...");
   if (!runAsync(String.format(LAUNCH_CLIENT_FORMAT, aiDirectory, port), aiDirectory))
     return false;

   return true;
 }

 private static boolean run(String command, String directory) {
   DefaultExecutor executor = getExecutor(directory);
   System.out.println("Running " + command);
   CommandLine commandLine = CommandLine.parse(command);
   try {
     executor.execute(commandLine);
   }
   catch (ExecuteException e) {
     System.out.println("Failed to execute " + command);
     return false;
   }
   catch (IOException e) {
     System.out.println("IO Exception running " + command);
     return false;
   }
   return true;
 }

 private static DefaultExecutor getExecutor(String directory) {
   ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
   PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
   DefaultExecutor executor = new DefaultExecutor();
   executor.setWorkingDirectory(new File(directory));
   executor.setStreamHandler(streamHandler);
   return executor;
 }

 private static boolean runAsync(String command, String directory) {

   CommandLine commandLine = CommandLine.parse(command);
   System.out.println("Running async " + command);
   DefaultExecutor executor = getExecutor(directory);
   DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
   try {
     executor.execute(commandLine, resultHandler);
   }
   catch (ExecuteException e) {
     System.out.println("Failed to execute " + command);
     return false;
   }
   catch (IOException e) {
     System.out.println("IO Exception running " + command);
     return false;
   }
   return true;
 }

1 个答案:

答案 0 :(得分:2)

这是你的回调:

DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

这是您的异步​​执行程序:

executor.execute(commandLine, resultHandler);

但是,在异步模式下,在调用executor.execute()之后,该方法将继续,但在另一个线程中发生某些事情并且resultHandler.onProcessComplete()resultHandler.onProcessFailed()被调用之前,您仍然不会知道执行是否结束,并且不应该退出runAsync()方法。

我相信将你的方法改为这样的方法会有效:

private static boolean runAsync(String command, String directory) {

   CommandLine commandLine = CommandLine.parse(command);
   System.out.println("Running async " + command);
   DefaultExecutor executor = getExecutor(directory);
   DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
   try {
     executor.execute(commandLine, resultHandler);
     resultHandler.waitFor();
     if(resultHandler.getException() != null){
       throw resultHandler.getException();
     }
   }
   catch (ExecuteException e) {
     System.out.println("Failed to execute " + command);
     return false;
   }
   catch (IOException e) {
     System.out.println("IO Exception running " + command);
     return false;
   }
   return true;
 }

因为resultHandler.waitFor()会使线程等到执行结束。

但这与使用同步模式相同。 只有当两个调用都是异步的时候,你才会赢得性能,因为它们会并行运行,你会等到两个完成。