首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用异步TaskManager处理作业/步骤异常

使用异步TaskManager处理作业/步骤异常
EN

Stack Overflow用户
提问于 2016-02-24 19:01:47
回答 1查看 2.5K关注 0票数 0

我找不到一种合适的方法来处理异步上下文中的spring-batch异常。

当我将ThreadPoolTaskManager设置为我的JobLauncher时,不再记录实际的作业/步骤异常。相反,日志将如下所示:

代码语言:javascript
复制
org.springframework.batch.core.JobInterruptedException: Job interrupted by step execution
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:165)
at ...

我试图像这样添加一个JobExecutionListener来解决这个问题:

代码语言:javascript
复制
@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作为结尾。有没有办法检索中断的初始原因(可能是读取器/处理器/写入器代码中的错误?

谢谢,

西里尔

EN

回答 1

Stack Overflow用户

发布于 2016-04-20 23:15:31

所以答案很简单,当我理解它的时候,我觉得自己真的很愚蠢:@Artefacto是正确的。作业已停止。在这个过程的最后。因为它到达了main()方法的末尾。

当我用ThreadPoolTaskManager切换到异步模式时,我忘了在main方法中添加一行非常重要的代码:

代码语言:javascript
复制
    // Wait for the end of the JobExecution
    main.endOfJobLatch.await();

希望这个答案能帮助其他人……

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35600278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档