I'm reading a multi-format file using FlatFileItemReader
and mapping every line to its corresponding bean type in ItemProcessor
and performing data enrichment. But when I try to write these records into a file using FlatFileItemWriter
, I'm not able to assign separate BeanWrapperFieldExtractor
for the different record types. How can I sort this out?
Input File format
1#9999999#00001#2#RecordType1
2#00002#June#Statement#2020#9#RecordType2
3#7777777#RecordType3
Expected output file format
1#9999999#00001#2#RecordType1#mobilenumber1
2#00002#June#Statement#2020#9#RecordType2#mobilenumber2
3#7777777#RecordType3#mobilenumber3
ItemProcessor
public class RecordTypeItemProcessor implements ItemProcessor<RecordType, RecordType> {
@Override
public RecordType process(RecordType recordType) throws Exception {
if (recordType instanceof RecordType1) {
RecordType1 recordType1 = (RecordType1) recordType;
//enrichment logic
return recordType1;
} else if (recordType instanceof RecordType2) {
RecordType2 recordType2 = (RecordType2) recordType;
//enrichment logic
return recordType2;
}
else
return null;
}
}```
You can use a
ClassifierCompositeItemProcessor
with aClassifierCompositeItemWriter
for that. The idea is to classify items according to their type using aClassifier
and call the corresponding item processor/writer. This approach is cleaner IMO than using multipleinstance of
checks in the processor/writer.There are some built-in
Classifier
implementations (I thinkSubclassClassifier
is a good option for you), but you can create your own if needed.You can find examples in ClassifierCompositeItemProcessorTests and ClassifierCompositeItemWriterTests.