简体   繁体   English

在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>

Getting below Error : Invalid content was found starting with element skippable-exception-classes . 摆脱错误:找到无效的内容,从元素skippable-exception-classes开始。

On researching I found that skippable-exception-classes can be used within chunks. 在研究中,我发现可以在块内使用skippable-exception-classes But I need to achieve the same with ref tasklets. 但是我需要使用ref tasklet达到相同的目的。

If you want to Skip Exception in your own Tasklet implementation, you need to write the code to do so in within your Tasklet own implementation. 如果要在自己的Tasklet实现中Skip Exception ,则需要在Tasklet自己的实现中编写代码来做到这一点。

Follow this original thread and please Up Vote on original thread if this solution works for you. 遵循此原始主题 ,如果此解决方案适合您,请对原始主题进行投票

You can do something like 你可以做类似的事情

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

And in SpringXML configuration 并在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM