【问题标题】:Adding Skippable Exception Classes on Tasklet Impementation在 Tasklet 实现中添加可跳过的异常类
【发布时间】:2016-05-13 09:55:01
【问题描述】:
<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 tasklet 实现相同的目标。

【问题讨论】:

  • 通过 ref tasklet 我可以通过哪种方式实现相同的目标?

标签: java spring spring-batch


【解决方案1】:

如果您想在自己的Tasklet 实现中使用Skip Exception,则需要在您自己的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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多