Spring Batch – TaskletStep中的可跳过exception

如果发生某种exception,我试图让一个作业没有BatchStatus.FAILED

文档讨论了在使用skippable-exception-classes ,但是如何在TaskletStep做同样的TaskletStep呢? 以下代码不起作用:

          

我在Tasklet中实现了这个function,Michael Minella建议:

 abstract class SkippableTasklet implements Tasklet { //Exceptions that should not cause job status to be BatchStatus.FAILED private List> skippableExceptions; public void setSkippableExceptions(List> skippableExceptions) { this.skippableExceptions = skippableExceptions; } private boolean isSkippable(Exception e) { if (skippableExceptions == null) { return false; } for (Class c : skippableExceptions) { if (e.getClass().isAssignableFrom(c)) { return true; } } return true; } protected abstract void run(JobParameters jobParameters) throws Exception; @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { StepExecution stepExecution = chunkContext.getStepContext().getStepExecution(); JobExecution jobExecution = stepExecution.getJobExecution(); JobParameters jobParameters = jobExecution.getJobParameters(); try { run(prj); } catch (Exception e) { if (!isSkippable(e)) { throw e; } else { jobExecution.addFailureException(e); } } return RepeatStatus.FINISHED; } } 

以及示例SkippableTasklet的Spring XML配置:

     org.springframework.mail.MailException     

Tasklet ,exception处理的责任在于Tasklet的实现。 面向块的处理中可用的跳过逻辑是由ChunkOrientedTasklet提供的exception处理ChunkOrientedTasklet 。 如果要在自己的Tasklet实现中跳过exception,则需要在自己的实现中编写代码。