我找不到一种合适的方法来处理异步上下文中的spring-batch异常。
当我将ThreadPoolTaskManager设置为我的JobLauncher时,不再记录实际的作业/步骤异常。相反,日志将如下所示:
org.springframework.batch.core.JobInterruptedException: Job interrupted by step execution
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:165)
at ...我试图像这样添加一个JobExecutionListener来解决这个问题:
@Override
public void afterJob(JobExecution jobExecution) {
List<Throwable> jobExceptions = jobExecution.getFailureExceptions();
if (CollectionUtils.isNotEmpty(jobExceptions)) {
Throwable lastJobException = jobExceptions.get(jobExceptions.size() - 1);
LOGGER.error("Spring-Batch error at job level", lastJobException);
String lastJobExceptionMessage = ExceptionUtils.getRootCauseMessage(lastJobException);
// storing message in ExecutionContext for the batch-admin webapp
String message = "";
if (jobExecution.getExecutionContext().get(Consts.JOB_EXECUTION_MESSAGE_KEY) != null) {
message = jobExecution.getExecutionContext().getString(Consts.JOB_EXECUTION_MESSAGE_KEY);
}
message += "\n" + lastJobExceptionMessage;
jobExecution.getExecutionContext().put(Consts.JOB_EXECUTION_MESSAGE_KEY, message);
}
}但我还是以JobInterruptedException作为结尾。有没有办法检索中断的初始原因(可能是读取器/处理器/写入器代码中的错误?
谢谢,
西里尔
发布于 2016-04-20 23:15:31
所以答案很简单,当我理解它的时候,我觉得自己真的很愚蠢:@Artefacto是正确的。作业已停止。在这个过程的最后。因为它到达了main()方法的末尾。
当我用ThreadPoolTaskManager切换到异步模式时,我忘了在main方法中添加一行非常重要的代码:
// Wait for the end of the JobExecution
main.endOfJobLatch.await();希望这个答案能帮助其他人……
https://stackoverflow.com/questions/35600278
复制相似问题