3333import java .util .ArrayList ;
3434import java .util .HashMap ;
3535import java .util .List ;
36- import java .util .concurrent .ExecutorService ;
37- import java .util .concurrent .Executors ;
38- import java .util .concurrent .ThreadFactory ;
39- import java .util .concurrent .TimeUnit ;
36+ import java .util .concurrent .*;
4037import java .util .concurrent .atomic .AtomicInteger ;
4138
4239/**
@@ -275,7 +272,7 @@ public static String execCommand(String command) {
275272 logger .info ("execCommand [{}]" , command );
276273 try {
277274 return doExecCommand (command , null , 5 * TimeUtils .MILLIS_PER_MINUTE );
278- } catch (IOException | InterruptedException e ) {
275+ } catch (IOException | InterruptedException | ExecutionException e ) {
279276 throw new RuntimeException (e );
280277 }
281278 }
@@ -290,37 +287,21 @@ public static String execCommand(String command, String workingDirectory, long t
290287 var wd = new File (workingDirectory );
291288 try {
292289 return doExecCommand (command , wd , timeoutMillis );
293- } catch (IOException | InterruptedException e ) {
290+ } catch (IOException | InterruptedException | ExecutionException e ) {
294291 throw new RuntimeException (e );
295292 }
296293 }
297294
298- private static String doExecCommand (String command , File wd , long timeoutMillis ) throws IOException , InterruptedException {
295+ private static String doExecCommand (String command , File wd , long timeoutMillis ) throws IOException , InterruptedException , ExecutionException {
299296 var commandSplits = command .split (StringUtils .SPACE_REGEX );
300297 var process = new ProcessBuilder (commandSplits )
301298 .redirectErrorStream (true )
302299 .directory (wd )
303300 .start ();
304301
305- var stdout = new StringBuilder ();
306- var stderr = new StringBuilder ();
307-
308302 // 异步读取输出,避免缓冲区阻塞
309- executors .submit (ThreadUtils .safeRunnable (() -> {
310- try {
311- stdout .append (StringUtils .bytesToString (IOUtils .toByteArray (process .getInputStream ())));
312- } catch (IOException e ) {
313- throw new RuntimeException (e );
314- }
315- }));
316- executors .submit (ThreadUtils .safeRunnable (() -> {
317- try {
318- stderr .append (StringUtils .bytesToString (IOUtils .toByteArray (process .getErrorStream ())));
319- } catch (IOException e ) {
320- throw new RuntimeException (e );
321- }
322-
323- }));
303+ var stdoutFuture = executors .submit (ThreadUtils .safeCallable (() -> StringUtils .bytesToString (IOUtils .toByteArray (process .getInputStream ()))));
304+ var stderrFuture = executors .submit (ThreadUtils .safeCallable (() -> StringUtils .bytesToString (IOUtils .toByteArray (process .getErrorStream ()))));
324305
325306 var finished = process .waitFor (timeoutMillis , TimeUnit .MILLISECONDS );
326307 if (!finished ) {
@@ -329,6 +310,8 @@ private static String doExecCommand(String command, File wd, long timeoutMillis)
329310 }
330311
331312 process .destroy ();
313+ var stdout = stdoutFuture .get ();
314+ var stderr = stderrFuture .get ();
332315
333316 // 获取线程的退出值,0代表正常退出,非0代表异常中止
334317 int exitValue = process .exitValue ();
0 commit comments