I need to read data from db and then generate an XLSX file and update data in db.
No issue for the reader part.
For the writer part, I ended up creating a CompositeItemWriter, composed of a first Writer that generates the XLSX file and a second one that update data in db.
The problem: I need to intercept before- and after-step for the first writer in order to be able to properly write to the XLSX file in chunks. But, as soon as I compose both writers into a CompositeItemWriter, methods annotated with @BeforeStep et @AfterStep are not called.
Same issue when making the first writer implement StepExecutionListener.
Any idea why?
Here follows the CompositeItemWriter definition, the step and the writer:
public CompositeItemWriter<Adhesion> compositeItemWriter() {
CompositeItemWriter writer = new CompositeItemWriter();
writer.setDelegates(Arrays.asList(adhesionToXlsWriter, adhesionToDbWriter));
return writer;
}
@Bean
public Step xlsStep(ItemReader<Adhesion> adhesionFromDbReader) {
return stepBuilderFactory.get(JOB_NAME + "-step")
.<Adhesion, Adhesion>chunk(10)
.reader(adhesionFromDbReader)
.writer(compositeWriter())
.build();
}
@Component
public class AdhesionToXlsWriter implements ItemWriter<Adhesion> {
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
// !! never called !!
}
@Override
public void write(List<? extends Adhesion> items) throws Exception {
// generate XLSX content
}
@AfterStep
public ExitStatus afterStep(StepExecution stepExecution) {
// !! never called !!
}
}
You need to register the XslWriter as listener to the step. Use the "listener" method of the stepBuilder.
If a writer, reader, processor is "hidden" behind a composite class, the spring framework will not recognize it as listener automatically. therefore you have to do it "manually".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With