正如Michael Minella建议的那样,我在Tasklet中实现了此功能:
abstract class SkippableTasklet implements Tasklet { //Exceptions that should not cause job status to be BatchStatus.FAILED private List<Class<?>> skippableExceptions; public void setSkippableExceptions(List<Class<?>> 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; }}以Spring XML配置为例
SkippableTasklet:
<batch:tasklet> <bean scope="step" autowire="byType"> <property name="skippableExceptions"> <list> <value>org.springframework.mail.MailException</value> </list> </property> </bean></batch:tasklet>



