在 Tasklet 实现上添加可跳过的异常 类

Adding Skippable Exception Classes on Tasklet Impementation

<job id="pullPurgeProcessStoreFiles" xmlns="http://www.springframework.org/schema/batch">
    <bean id="PullFilesTasklet" class="com.example.PullFilesTasklet" />
         <step id="pullFiles" next="validation" >
            <tasklet ref="PullFilesTasklet">
                <skippable-exception-classes>
                    <include class="java.io.FileNotFoundException"/>
                </skippable-exception-classes>
            </tasklet>  
        </step>     
</job>

低于错误: 发现以元素 skippable-exception-classes 开头的无效内容。

在研究中我发现 skippable-exception-classes 可以在块中使用。但是我需要用 ref tasklets 来达到同样的效果。

如果您想 Skip Exception 在您自己的 Tasklet 实现中,您需要在您的 Tasklet 自己的实现中编写代码来实现。

关注原版 thread,如果此解决方案适合您,请在原版帖子中投票

你可以这样做

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;
    }
}

并且在 SpringXML 配置中

<batch:tasklet>
    <bean class="com.MySkippableTasklet" scope="step" autowire="byType">
        <property name="skippableExceptions">
            <list>
                <value>org.springframework.mail.MailException</value>
            </list>
        </property>
    </bean>
</batch:tasklet>