Tuesday, April 27, 2010

Spring Batch - ItemReader and ItemWriter configuration

How to read a record to a object
ItemReader will utilize LineMapper, LineTokenizer and FieldSetMapper to get a Object.
LineMapper: DefaultLineMapper, PattermMatchingCompositeLineMapper
LineTokenizer: DelimitedLineTokenizer, FixedLengthTokenizer, PattermMatchingCompositeLineTokenizer
FieldSetMapper: BeanWrapperFieldSetMapper
Configuration Hierarchy:

Multiple record type with single file

It is very common that a file might have records with different formats that need to be tokenized differently and mapped to different objects.


The PatternMatchingCompositeLineMapper makes this easy by allowing maps of patterns to LineTokenizers and patterns to FieldSetMappers to be configured:

<bean id="orderFileLineMapper" class="org.spr...PatternMatchingCompositeLineMapper">
  <property name="tokenizers">
      <entry key="USER*" value-ref="userTokenizer" />
      <entry key="LINEA*" value-ref="lineATokenizer" />
      <entry key="LINEB*" value-ref="lineBTokenizer" />
  <property name="fieldSetMappers">
      <entry key="USER*" value-ref="userFieldSetMapper" />
      <entry key="LINE*" value-ref="lineFieldSetMapper" />

How to write a file
The ItemWriter will leverage the LineAggregator and FieldExtractor to write out the data to file.
LineAggregator: PassThroughLineAggregator, DelimitedLineAggregator
FieldExtractor: BeanWrapperFieldExtractor

Configuration Hierarchy: