skip FlatFileParseException or specific exception in Spring Batch

社会主义新天地 提交于 2019-12-24 00:44:34

问题


Hi I have requirement to read (n number) of flat file. During file reading if received FileParseException: from reader then stop the current file reading and came out safely and process next file and continue the job execution. currently i have this xml config but i don't want to go with this because i don't have a really skip limit count. is there any way to handle this scenario may be using ItemReaderListener ?

<chunk reader="flatFileItemReader" writer="itemWriter"
             commit-interval="10" skip-limit="2">
         <skippable-exception-classes>
            <include class="org.springframework.batch.item.file.FlatFileParseException"/>
         </skippable-exception-classes>

回答1:


Instead of specifying a skip-limit, you can use a policy. There are several out-of-the-box skip policies, it sounds like you always want to skip (no limit), use AlwaysSkipItemSkipPolicy.

Example config :

<batch:skip-policy>   

     <bean:bean class="org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy"/>

</batch:skip-policy> 



回答2:


thanks Doeleman, based upon your input i am able to skip exception usingAlwaysSkipItemSkipPolicy this is how i have implemented

public class SkipPolicy extends  AlwaysSkipItemSkipPolicy  {

    @Override
     public boolean shouldSkip(java.lang.Throwable t, int skipCount){

        if(t instanceof NonSkippableReadException){
            return true;
        }
        return false;

     }
}

xml config.

<batch:chunk reader="cvsFileItemReader"  writer="mysqlItemWriter" 
                    commit-interval="2" skip-policy="mySkipPolicy">

<bean id="mySkipPolicy" class="com.model.SkipPolicy"/>


来源:https://stackoverflow.com/questions/35659738/skip-flatfileparseexception-or-specific-exception-in-spring-batch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!