Question

I want to use Spring Batch to read a list of files and process them as an item for each row in the file. At the end of the file I want a count of the rows in the file.

Which API do I use in Spring Batch to know that an item represents the end of a file, before the next one begins?

Was it helpful?

Solution

I suppose you are working with record-based files.
Using a MultiResourceItemReader (to read multiple files) with a FlatFileItemReader (to parse file).
Our intention is to detect when a file reach the end so with a PeekableItemReader we look-ahead for next record and if this is null we meet the EOF

class EOFCheckItemReader<T> extends SingleItemPeekableItemReader<T> {
  private int count;
  T read() {
    T item = super.read();
    if(null == item) {
      //  EOF detected
      manageEOF();
      count = 0;
    }
    else {
      count++;
    }
    return item;
  }

  private void manageEOF() {}

  // Also implements update(), open(), close() 
  // to save count value into execution context
  // No need to save delegate's state because we register it
  // as stream into job's xml definition
}

Spring batch is heavly based on delegation so, using property "delegate" of MultiResourceItemReader or SingleItemPeekableItemReader we can solve easly the problem. This is a simple skeleton (just an idea, I can't test it sorry):

<bean name="csvReader" class="FlatFileItemReader">
  <!-- set all properties -->
</bean>
<bean name="eofCheckReader" class="EOFCheckItemReader">
  <property name="delegate" ref="multiResourceReader" />
</bean>
<bean name="multiResourceReader">
  <property name="resources" value="file://*.csv" />
  <property name="delegate" ref="csvReader" />
</bean>
<step>
  <chunk reader="eofCheckReader" />
  <streams>
    <stream ref="multiResourceReader" />
  </streams>
</step>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top