|
|
@ -41,6 +41,7 @@ import java.util.Collections; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.LinkedList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.function.Consumer; |
|
|
|
import java.util.function.Consumer; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Matcher; |
|
|
@ -67,7 +68,7 @@ public abstract class AbstractCommandExecutor { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* log handler |
|
|
|
* log handler |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected Consumer<List<String>> logHandler; |
|
|
|
protected Consumer<LinkedBlockingQueue<String>> logHandler; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* logger |
|
|
|
* logger |
|
|
@ -75,9 +76,9 @@ public abstract class AbstractCommandExecutor { |
|
|
|
protected Logger logger; |
|
|
|
protected Logger logger; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* log list |
|
|
|
* log collection |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected final List<String> logBuffer; |
|
|
|
protected final LinkedBlockingQueue<String> logBuffer; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* taskExecutionContext |
|
|
|
* taskExecutionContext |
|
|
@ -92,13 +93,13 @@ public abstract class AbstractCommandExecutor { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private TaskExecutionContextCacheManager taskExecutionContextCacheManager; |
|
|
|
private TaskExecutionContextCacheManager taskExecutionContextCacheManager; |
|
|
|
|
|
|
|
|
|
|
|
public AbstractCommandExecutor(Consumer<List<String>> logHandler, |
|
|
|
public AbstractCommandExecutor(Consumer<LinkedBlockingQueue<String>> logHandler, |
|
|
|
TaskExecutionContext taskExecutionContext , |
|
|
|
TaskExecutionContext taskExecutionContext, |
|
|
|
Logger logger){ |
|
|
|
Logger logger) { |
|
|
|
this.logHandler = logHandler; |
|
|
|
this.logHandler = logHandler; |
|
|
|
this.taskExecutionContext = taskExecutionContext; |
|
|
|
this.taskExecutionContext = taskExecutionContext; |
|
|
|
this.logger = logger; |
|
|
|
this.logger = logger; |
|
|
|
this.logBuffer = Collections.synchronizedList(new ArrayList<>()); |
|
|
|
this.logBuffer = new LinkedBlockingQueue<>(Constants.DEFAULT_LOG_BUFFER_SIZE); |
|
|
|
this.taskExecutionContextCacheManager = SpringApplicationContext.getBean(TaskExecutionContextCacheManagerImpl.class); |
|
|
|
this.taskExecutionContextCacheManager = SpringApplicationContext.getBean(TaskExecutionContextCacheManagerImpl.class); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -331,15 +332,14 @@ public abstract class AbstractCommandExecutor { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void clear() { |
|
|
|
private void clear() { |
|
|
|
|
|
|
|
|
|
|
|
List<String> markerList = new ArrayList<>(); |
|
|
|
LinkedBlockingQueue<String> markerLog = new LinkedBlockingQueue<>(1); |
|
|
|
markerList.add(ch.qos.logback.classic.ClassicConstants.FINALIZE_SESSION_MARKER.toString()); |
|
|
|
markerLog.add(ch.qos.logback.classic.ClassicConstants.FINALIZE_SESSION_MARKER.toString()); |
|
|
|
|
|
|
|
|
|
|
|
if (!logBuffer.isEmpty()) { |
|
|
|
if (!logBuffer.isEmpty()) { |
|
|
|
// log handle
|
|
|
|
// log handle
|
|
|
|
logHandler.accept(logBuffer); |
|
|
|
logHandler.accept(logBuffer); |
|
|
|
logBuffer.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
logHandler.accept(markerList); |
|
|
|
logHandler.accept(markerLog); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -350,9 +350,7 @@ public abstract class AbstractCommandExecutor { |
|
|
|
String threadLoggerInfoName = String.format(LoggerUtils.TASK_LOGGER_THREAD_NAME + "-%s", taskExecutionContext.getTaskAppId()); |
|
|
|
String threadLoggerInfoName = String.format(LoggerUtils.TASK_LOGGER_THREAD_NAME + "-%s", taskExecutionContext.getTaskAppId()); |
|
|
|
ExecutorService getOutputLogService = ThreadUtils.newDaemonSingleThreadExecutor(threadLoggerInfoName + "-" + "getOutputLogService"); |
|
|
|
ExecutorService getOutputLogService = ThreadUtils.newDaemonSingleThreadExecutor(threadLoggerInfoName + "-" + "getOutputLogService"); |
|
|
|
getOutputLogService.submit(() -> { |
|
|
|
getOutputLogService.submit(() -> { |
|
|
|
BufferedReader inReader = null; |
|
|
|
try (BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) { |
|
|
|
try { |
|
|
|
|
|
|
|
inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
|
|
|
|
|
|
|
String line; |
|
|
|
String line; |
|
|
|
while ((line = inReader.readLine()) != null) { |
|
|
|
while ((line = inReader.readLine()) != null) { |
|
|
|
logBuffer.add(line); |
|
|
|
logBuffer.add(line); |
|
|
@ -361,7 +359,6 @@ public abstract class AbstractCommandExecutor { |
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
logOutputIsSuccess = true; |
|
|
|
logOutputIsSuccess = true; |
|
|
|
close(inReader); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
getOutputLogService.shutdown(); |
|
|
|
getOutputLogService.shutdown(); |
|
|
@ -450,31 +447,20 @@ public abstract class AbstractCommandExecutor { |
|
|
|
* @return line list |
|
|
|
* @return line list |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private List<String> convertFile2List(String filename) { |
|
|
|
private List<String> convertFile2List(String filename) { |
|
|
|
List lineList = new ArrayList<String>(100); |
|
|
|
List<String> lineList = new ArrayList<>(100); |
|
|
|
File file=new File(filename); |
|
|
|
File file = new File(filename); |
|
|
|
|
|
|
|
|
|
|
|
if (!file.exists()){ |
|
|
|
if (!file.exists()){ |
|
|
|
return lineList; |
|
|
|
return lineList; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
BufferedReader br = null; |
|
|
|
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), StandardCharsets.UTF_8))) { |
|
|
|
try { |
|
|
|
|
|
|
|
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
String line = null; |
|
|
|
String line = null; |
|
|
|
while ((line = br.readLine()) != null) { |
|
|
|
while ((line = br.readLine()) != null) { |
|
|
|
lineList.add(line); |
|
|
|
lineList.add(line); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error(String.format("read file: %s failed : ",filename),e); |
|
|
|
logger.error(String.format("read file: %s failed : ", filename), e); |
|
|
|
} finally { |
|
|
|
|
|
|
|
if(br != null){ |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
br.close(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
logger.error(e.getMessage(),e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return lineList; |
|
|
|
return lineList; |
|
|
|
} |
|
|
|
} |
|
|
@ -551,27 +537,10 @@ public abstract class AbstractCommandExecutor { |
|
|
|
lastFlushTime = now; |
|
|
|
lastFlushTime = now; |
|
|
|
/** log handle */ |
|
|
|
/** log handle */ |
|
|
|
logHandler.accept(logBuffer); |
|
|
|
logHandler.accept(logBuffer); |
|
|
|
|
|
|
|
|
|
|
|
logBuffer.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return lastFlushTime; |
|
|
|
return lastFlushTime; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* close buffer reader |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param inReader in reader |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void close(BufferedReader inReader) { |
|
|
|
|
|
|
|
if (inReader != null) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
inReader.close(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
logger.error(e.getMessage(), e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected List<String> commandOptions() { |
|
|
|
protected List<String> commandOptions() { |
|
|
|
return Collections.emptyList(); |
|
|
|
return Collections.emptyList(); |
|
|
|
} |
|
|
|
} |
|
|
|