Below is the step code which will get initialized in job.
@Bean
public Step stepOne() {
return stepBuilderFactory.get("stepOne")
.<EmployeeDTO, EmployeeDTO> chunk(1)
.reader(readerOne())
.processor(processorOne())
.writer(compositeWriter())
.build();
}
Below is the composite writer for writting the data.
@Bean
public CompositeItemWriter<EmployeeDTO> compositeWriter(){
return new CompositeWriter();
}
public class CompositeWriter extends CompositeItemWriter<EmployeeDTO> {
@Autowired
private DataSource dataSource;
List<EmployeeDTO> insert;
List<EmployeeDTO> update;
@Override
public void write(List<? extends EmployeeDTO> arg0) throws Exception {
CompositeItemWriter<EmployeeDTO> compositeItemWriter = new CompositeItemWriter<>();
compositeItemWriter.setDelegates(Arrays.asList(insertW(),updateW()));
}
private JdbcBatchItemWriter<EmployeeDTO> insertW() throws Exception{
JdbcBatchItemWriter<EmployeeDTO> batchItemWriter = new JdbcBatchItemWriter<>();
batchItemWriter.setDataSource(dataSource);
batchItemWriter.setSql("");
batchItemWriter.write(insert);
return batchItemWriter;
}
private JdbcBatchItemWriter<EmployeeDTO> updateW() throws Exception{
JdbcBatchItemWriter<EmployeeDTO> batchItemWriter = new JdbcBatchItemWriter<>();
batchItemWriter.setDataSource(dataSource);
batchItemWriter.setSql("");
batchItemWriter.write(update);
return batchItemWriter;
}
}
There are a couple of issues with your configuration:
write
method on the writer (batchItemWriter.write(insert)
and batchItemWriter.write(update)
), Spring Batch will do it.CompositeItemWriter
. You can create an instance an register delegate writers in it.Here is how to proceed in your case:
@Bean
public JdbcBatchItemWriter<EmployeeDTO> insertW() throws Exception{
JdbcBatchItemWriter<EmployeeDTO> batchItemWriter = new JdbcBatchItemWriter<>();
batchItemWriter.setDataSource(dataSource);
batchItemWriter.setSql("");
return batchItemWriter;
}
@Bean
public JdbcBatchItemWriter<EmployeeDTO> updateW() throws Exception{
JdbcBatchItemWriter<EmployeeDTO> batchItemWriter = new JdbcBatchItemWriter<>();
batchItemWriter.setDataSource(dataSource);
batchItemWriter.setSql("");
return batchItemWriter;
}
CompositeItemWriter
@Bean
public CompositeItemWriter<EmployeeDTO> compositeItemWriter() {
CompositeItemWriter<EmployeeDTO> compositeItemWriter = new CompositeItemWriter<>();
compositeItemWriter.setDelegates(Arrays.asList(insertW(), updateW()));
return compositeItemWriter;
}
@Bean
public Step stepOne() {
return stepBuilderFactory.get("stepOne")
.<EmployeeDTO, EmployeeDTO> chunk(1)
.reader(readerOne())
.processor(processorOne())
.writer(compositeItemWriter())
.stream(insertW())
.stream(updateW())
.build();
}
Hope this helps.
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