Issue
I am Using MultiResourceItemReader to read the multiple resources which is setting to the Resource[], and by using Fixed Length Tokenizer I am able to set the data to the POJO class. in the Item Processor I am able to get the Object which has been created by Item Reader and able to get the data from the object.
The Challenging which I am facing is to get the File Name for the specific object which has been created or processed and passed to the Item Processor.
I required store the File Name also along with the specific object data in to the database.
Appreciate the help on this, Thank you
*Here is the batch configuration *
@Configuration
@EnableBatchProcession
public class BatchConfig {
@Value("")
private Resource[] resource;
/* Job */
public Job job(){
return jobBuilderFactory.get("job").listerner(listener()).incrementer(new
RunIdIncrementer())
.start(step()).build();
}
/* Step */
public Step step(){
return stepBuilderFactory.get("step")<mypojo,mypojo>chunk(10)
.reader(multiREsourceItemReader()).processor(processor()).writer(writer()).build();
}
/* Multi Resource Item Reader */
@Bean
@StepScope
public MultiResourceItemReader<mypojo> multiResourceItemReader() {
MultiResourceItemReader<mypojo> multireader = new MultiResourceItemReader<mypojo>();
multireader.setResource(resources);
multireader.setDelegate(reader());
return mutlireader;
}
/* Flat FIle Item Reader */
@Bean
public FlatFileItemReader<mypojo> reader() {
FlatFileItmeReader<mypojo> reader = new FlatFileItemReader<mypojo>();
reader.setLineMapper(new DefaultLineMapper() {
{
setLineTokenizer(new FixedLengthTokenizer(){ {
setName(new String[]{"my pojo class attributes ..."});
setColumns( new Range { new Range[1,3] .....});
setStrickt(false);
}
});
setFieldSetMapper(new BeanWrapperFieldsSetMapper<mypojo>(){
{
setTargetType(AckResponse.class):
}
}); }});
return reader;
}
@Bean
public CustomItemProcessor processor(){
return new ResponseFileProcessor();
}
@Bean
public CustomFileWriter<mypojo> writer(){
return new CustomFileWriter<mypojo>();
}
}
/ Here is the Item Processor /
public class CustomProcessor implements ItemProcessor<mypojo,mypojo>{
public mypojo process(mypojo item) thows Exception {
return item;
}
Solution
I have figured out the solution by using ResourceAware Interface in Spring Batch. Mypojo requires to implement the ResourceAware and overried the setResource method and we can get the Resource details over there.
Regarding ResourceAware : https://docs.spring.io/spring-batch/docs/current/api/org/springframework/batch/item/ResourceAware.html
Answered By - Mahesh
Answer Checked By - Terry (JavaFixing Volunteer)