Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Spring batch CompositeItemWriter with different data and having two JdbcBatchItemWritter and in Java configurations without xml

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;
   }
}
like image 431
Sopaneel Maskar Avatar asked Nov 24 '18 16:11

Sopaneel Maskar


1 Answers

There are a couple of issues with your configuration:

  • You don't need to call the write method on the writer (batchItemWriter.write(insert) and batchItemWriter.write(update)), Spring Batch will do it.
  • There is no need to subclass the CompositeItemWriter. You can create an instance an register delegate writers in it.
  • Delegate writers need to be registered as streams in the step.

Here is how to proceed in your case:

1. Define delegate writers

@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;
}

2. Register delegates in a CompositeItemWriter

@Bean
public CompositeItemWriter<EmployeeDTO> compositeItemWriter() {
    CompositeItemWriter<EmployeeDTO> compositeItemWriter = new CompositeItemWriter<>();
    compositeItemWriter.setDelegates(Arrays.asList(insertW(), updateW()));
    return compositeItemWriter;
}

3. Register delegates as streams in the step (if they implement ItemSteam)

@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.

like image 199
Mahmoud Ben Hassine Avatar answered Oct 26 '22 23:10

Mahmoud Ben Hassine