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,则需要在自己的实现中编写代码。