为什么业务'catch块没有捕获Spring Batch异常?

时间:2017-06-08 16:09:59

标签: spring spring-batch

我正在使用Spring Batch 3.0.7

我有以下代码:

    JobExecution jobExecution = null;

    try{
        jobExecution = jobLauncher.run(job, jobParameters);
        logger.info("jobExecution.getExitStatus().getExitCode(): {}", jobExecution.getExitStatus().getExitCode());
    }
    catch(Throwable t){
        logger.error("Throwable: {}", t.getClass().getSimpleName());
        logger.error("{}", t.getMessage());
        logger.info("jobExecution.getExitStatus().getExitCode(): {}", jobExecution.getExitStatus().getExitCode());
    }

由于实验场景,当Job处理特定记录时,ItemProcessor执行必须失败,因此抛出了自定义异常(PersonBatchException):

44207 [RMI TCP Connection(3)-192.168.1.8] ERROR o.s.batch.core.step.AbstractStep - Encountered an error executing step one in job simpleDatabaseJdbcReaderErrorProcessorDatabaseJdbcWriter 
com.manuel.jordan.batch.exception.PersonBatchException: The record 999 has been selected to fail
    at com.manuel.jordan.batch.processor.PersonExceptionItemProcessor.process(PersonExceptionItemProcessor.java:36)

在这种情况下,catch块应该可以工作,但它永远不会被执行。

当然,控制台会打印上面显示的所有错误堆栈跟踪。

即使使用<skippable-exception-classes>,我们也会假设会引发异常,但未在上述部分中列出。

我们假设通过jobLauncher.run(job, jobParameters)或特殊JMX流程调用@Service

如何处理?

α

public class PersonBatchException extends RuntimeException {

    private static final long serialVersionUID = -7424725101950190824L;

    public PersonBatchException(String message){
        super(message);
    }

}

1 个答案:

答案 0 :(得分:0)

感谢这篇文章:

我有这个版本:

    JobExecution jobExecution = null;

    try{
        jobExecution = jobLauncher.run(job, jobParameters);
        logger.info("Try jobExecution.getExitStatus().getExitCode(): {}", jobExecution.getExitStatus().getExitCode());
    }
    catch(RuntimeException re){
        logger.error("NEVER NEVER EXECUTED NEVER NEVER");
        logger.error("RuntimeException Class: {}", re.getClass().getSimpleName());
        logger.error("{}", re.getMessage());
        logger.error("Catch jobExecution.getExitStatus().getExitCode(): {}", jobExecution.getExitStatus().getExitCode());
    }

    if(jobExecution.getExitStatus().equals(ExitStatus.FAILED)) {
        List<Throwable> exceptions = jobExecution.getAllFailureExceptions();
        for(Throwable throwable : exceptions){
            logger.error("Throwable Class: {}", throwable.getClass().getSimpleName());
            logger.error("{}", throwable.getMessage());
        }
    }

catch阻止从不工作,但通过for阻止可能与catch阻止的方式基本相同。

似乎Spring Batch的内部流程以某种方式工作,将此行为反映到catch