Spring Batch ResourceAware - Get resource name in ClassifierCompositeItemWriter

807 views Asked by At

I've implemented ResourceAware in my domain object to set resource name when using MultiResourceItemReader. I'm successfully able to retrieve the resource name in Processor. However, I'm not able to access the resource name in FlatFileItemWriter since I don't have access to the domain object, after classifying using ClassifierCompositeItemWriter. Any help would be much appreciated.

Configuration

 stepBuilderFactory.get("enrichmentStep")
.<RecordType, RecordType>chunk(1)
.reader(multiResourceItemReader())
.processor(processor())
.writer(compositeWriter())
.stream(type0Writer())
.stream(type1Writer())
.stream(type2Writer())

Writer

public ClassifierCompositeItemWriter compositeWriter() throws Exception {
  ClassifierCompositeItemWriter<RecordType> writer = new ClassifierCompositeItemWriter<>();
  SubclassClassifier classifier = new SubclassClassifier<>();
  Map typeMap = new HashMap<>();
  typeMap.put(RecordType0.class, type0Writer());
  typeMap.put(RecordType1.class, type1Writer());
  typeMap.put(RecordType2.class, type2Writer());
  classifier.setTypeMap(typeMap);
  writer.setClassifier(classifier);
  return writer;
}

@Bean
public FlatFileItemWriter<RecordType0> type0Writer() throws Exception{
  FlatFileItemWriter<RecordType0> writer = new FlatFileItemWriter<>();
  writer.setResource( new FileSystemResource("")); //To get the resource value from domain object here
  writer.setAppendAllowed(true);
  writer.setLineAggregator(new DelimitedLineAggregator<RecordType0>() {{
  setDelimiter("#");
  setFieldExtractor(new BeanWrapperFieldExtractor<RecordType0>() {{
  setNames(new String[] { "RecordType", "BatchID", "SubBatchID"});}});}});
   return  writer;
}
0

There are 0 answers