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

时间:2016-05-13 09:55:02

标签: java spring spring-batch

ReportViewer1.PageCountMode = new PageCountMode();

获得以下错误: 找到以元素<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可以在块内使用。但我需要使用ref tasklets实现相同的目标。

1 个答案:

答案 0 :(得分:3)

如果您想在自己的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>