我有一个使用 processbuilder 启动流程的应用程序。processBuilder 看起来像这样
public int runCommandWithLog(String logFile, List<String> command) throws IOException, InterruptedException {
ProcessBuilder pb = new ProcessBuilder(command)
.redirectOutput(ProcessBuilder.Redirect.appendTo(new File(logFile + ".log")))
.redirectError( ProcessBuilder.Redirect.appendTo(new File(logFile + ".err")))
.redirectErrorStream(true);
logger.info("Starting the batch file with the logger function");
Process p = pb.start();
p.waitFor(15, TimeUnit.MINUTES);
int exitCode = p.exitValue();
logger.info("Exit Code for the Execution {}" ,exitCode);
p.destroy();
return exitCode;
}
我调用这个“runCommandWithLog”方法的方式是使用线程的运行方法,该方法看起来像这样
public void run() {
try {
logger.info("Starting a thread for the batch operattion {}",Thread.currentThread().getName());
filePath = filePath+DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now());
logger.info("Calling batch executor");
exitCode= runCommandWithLog(filePath,command);
if (exitCode != 0) {
logger.error(String.format("Failure in executing script: %s", scriptPath));
}else{
logger.info("Batch file execution has completed successfully, starting the Kafka Consumer now");
String response = processMessagesafterBatch();
logger.info("Stopping the execution of the current thread {} after getting response {}",Thread.currentThread().getName(),response);
executorService.shutdown();
}
}catch (Exception e) {
e.printStackTrace();
return;
}
}
我的问题是当我在更高的环境中时,我能够执行一次代码,即当我启动 api 时,它执行批处理文件,等待它完成然后调用 processMessagesafterBatch() 方法得到响应和也会关闭 executorservice。但是,我无法在不退回应用程序的情况下再次请求相同的操作。当我检查日志时,第二次运行得到的最后一件事是来自 runCommandWithLog 方法的日志
("Starting the batch file with the logger function")
我错过了什么吗?为什么我不能多次运行 processbuilder?