Search code examples
springfilespring-batchskip

Spring Batch - Validate Header Lines in input csv file and skip the file if it invalidates


I have a simple job as below:

<batch:step id="step">
 <batch:tasklet>
  <batch:chunk reader="itemReader" processor="itemProcessor" writer="itemWriter" commit-    interval="5000" />
 </batch:tasklet>
</batch:step>

itemReader is as below:

<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
 <property name="linesToSkip" value="1"></property>
 <property name="skippedLinesCallback" ref="skippedLinesCallback" ></property>

 <property name="lineMapper">
    <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer" ref="lineTokenizer">                
                <property name="delimiter" value="," />                    
            </bean>
        </property>
        <property name="fieldSetMapper">
            <bean
                class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />
        </property>
    </bean>
</property>
<property name="resource" value="#{stepExecutionContext['inputKeyName']}" />
</bean>

<bean id"lineTokenizer"                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

<bean id="skippedLinesCallback" class="com.test.IteMReaderHeader" >
<property name="lineTokenizer" ref="lineTokenizer">
</bean>

I am setting the "names" of the input fields in "com.test.IteMReaderHeader" class by injecting "lineTokenizer" in it.

I need to validate the header lines which is the 1st line in the input csv file with a fixed header value and if the header line invalidates then in that case I need to fail the step and skip the entire file so that the next file can be used for reading.

Please suggest a suitable way of achieving it. I would really appreciate your time and valuable inputs.

Thanks !!


Solution

  • Looking code of FlatFileItemReader file stop condition is managed;

    1. with private field boolean noInput
    2. with private function readLine() used in protected doRead()

    IMHO the best solution is to throw a runtime exception from your skippedLineCallback and manage error as reader exhaustion condition.

    Foe example writing your delegate in this way

    class SkippableItemReader<T> implements ItemStreamReader<T> {
      private ItemStreamReader<T> flatFileItemReader;
      private boolean headerError = false;
    
      void open(ExecutionContext executionContext) throws ItemStreamException {
        try {
          flatFileItemReader.open(executionContext);
        } catch(MyCustomExceptionHeaderErrorException e) {
          headerError = true;
        }
      }
    
      public T read() {
        if(headerError)
          return null;
        return flatFileItemReader.read();
      }
    
      // Other functions delegation
    }
    

    (you have to register delegate as stream manually,of course)
    or extending FlatFileItemReader as

    class SkippableItemReader<T> extends FlatFileItemReader<T> {
      private boolean headerError = false;
    
      protected void doOpen() throws Exception {
        try {
          super.doOpen();
        } catch(MyCustomExceptionHeaderErrorException e) {
          headerError = true;
        }
      }
    
      protected T doRead() throws Exception {
        if(headerError)
          return null;
        return super.doRead();
      }    
    }
    

    The code has been written directly without test so there can be errors, but I hope you can understand my point.
    Hope can solve your problem