1919import java .nio .file .Files ;
2020import java .util .Set ;
2121import java .util .concurrent .ForkJoinPool ;
22+ import java .util .concurrent .ForkJoinTask ;
2223
2324/**
2425 * 脚本处理器
@@ -78,17 +79,23 @@ protected ProcessResult process0(TaskContext context) throws Exception {
7879 String result ;
7980
8081 final Charset charset = getCharset ();
81- try (InputStream is = process .getInputStream (); InputStream es = process .getErrorStream ()) {
82+ try {
83+ InputStream is = process .getInputStream ();
84+ InputStream es = process .getErrorStream ();
8285
83- POOL .execute (() -> copyStream (is , inputBuilder , omsLogger , charset ));
84- POOL .execute (() -> copyStream (es , errorBuilder , omsLogger , charset ));
86+ ForkJoinTask <?> inputSubmit = POOL .submit (() -> copyStream (is , inputBuilder , omsLogger , charset ));
87+ ForkJoinTask <?> errorSubmit = POOL .submit (() -> copyStream (es , errorBuilder , omsLogger , charset ));
8588
8689 success = process .waitFor () == 0 ;
8790
91+ // 阻塞等待日志读取
92+ inputSubmit .get ();
93+ errorSubmit .get ();
94+
8895 } catch (InterruptedException ie ) {
8996 omsLogger .info ("[SYSTEM] ScriptProcessor has been interrupted" );
9097 } finally {
91- result = String .format ("[INPUT]: %s;[ERROR]: %s" , inputBuilder . toString () , errorBuilder . toString () );
98+ result = String .format ("[INPUT]: %s;[ERROR]: %s" , inputBuilder , errorBuilder );
9299 }
93100 return new ProcessResult (success , result );
94101 }
@@ -132,11 +139,11 @@ private String prepareScriptFile(Long instanceId, String processorInfo) throws I
132139 return scriptPath ;
133140 }
134141
135- private static void copyStream (InputStream is , StringBuilder sb , OmsLogger omsLogger , Charset charset ) {
142+ private void copyStream (InputStream is , StringBuilder sb , OmsLogger omsLogger , Charset charset ) {
136143 String line ;
137144 try (BufferedReader br = new BufferedReader (new InputStreamReader (is , charset ))) {
138145 while ((line = br .readLine ()) != null ) {
139- sb .append (line );
146+ sb .append (line ). append ( System . lineSeparator ()) ;
140147 // 同步到在线日志
141148 omsLogger .info (line );
142149 }
@@ -145,6 +152,13 @@ private static void copyStream(InputStream is, StringBuilder sb, OmsLogger omsLo
145152 omsLogger .warn ("[SYSTEM] copyStream failed." , e );
146153
147154 sb .append ("Exception: " ).append (e );
155+ } finally {
156+ try {
157+ is .close ();
158+ } catch (IOException e ) {
159+ log .warn ("[ScriptProcessor] close stream failed." , e );
160+ omsLogger .warn ("[SYSTEM] close stream failed." , e );
161+ }
148162 }
149163 }
150164
0 commit comments