问题
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