Spring package how to skip the whole file provided

I have a Spring batch job that reads some files and saves to the database. If I need to be able to encode some user conditions and skip processing files if they do not meet this condition. I tried to extend the ItemReader and throw an exception, but this caused all the work to fail when I need a job to continue sorting through files.

thanks

+5
source share
3 answers

So, I ended up with the extension MultiResourceItemReader and overriding the read () method. Before delegating the file to the actual ItemReader, it checks the condition and transfers the file to the reader only if the condition is passed, otherwise processing the next file

+2
source

Take a look at the org.springframework.batch.core.step.skip.SkipPolicy interface. I will give an example from the Pro Spring Package written by T. Mineller

import java.io.FileNotFoundException; import org.springframework.batch.core.step.skip.SkipLimitExceededException; import org.springframework.batch.core.step.skip.SkipPolicy; import org.springframework.batch.item.ParseException; public class FileVerificationSkipper implements SkipPolicy { public boolean shouldSkip(Throwable exception, int skipCount) throws SkipLimitExceededException { if(exception instanceof FileNotFoundException) { return false; } else if(exception instanceof ParseException && skipCount <= 10) { return true; } else { return false; } } } 

Inside the xml file:

 <step id="copyFileStep"> <tasklet> <chunk reader="customerItemReader" writer="outputWriter" commit-interval="10" skip-limit="10"> <skippable-exception-classes> <include class="java.lang.Exception"/> <exclude class="org.springframework.batch.item.ParseException"/> </skippable-exception-classes> </chunk> </tasklet> </step> 

Or maybe another way could be to add at the beginning of your work a step that sorts your input files into two separate folders. Inside one folder, you will have all of your wrong files, and inside the other folder, only good ones will remain.

+3
source

override your reader’s doRead () method and throw a special exception for your application. For instance:

 *CustomFlatFileItemReader @Override protected T doRead() throws Exception { T itemRead=null; try { itemRead= super.doRead(); } catch (FlatFileParseException e) { throw new MyException(e.getMessage(), e); } return itemRead; }* 

2. Set skipPolicy jobs to skip MyException.

 *.skipPolicy((Throwable T, int skipCount) -> { if (T instanceof MyException) return true; else return false; }* 
+1
source

Source: https://habr.com/ru/post/1239043/


All Articles