27 Ağustos 2019 Salı

SpringBatch PoiItemReader Sınıfı

setLinesToSkip metodu
Şöyle yaparız.
@Bean
@StepScope
PoiItemReader<Foo> excelReader(@Value("#{jobParameters['fileName']}") String fileName) {
  PoiItemReader<Foo> reader = new PoiItemReader<>();
  reader.setLinesToSkip(3);
  reader.setResource(new ClassPathResource("data/" + fileName));
  reader.setRowMapper(excelRowMapper());
  return reader;
}

SpringBatch FlowJobBuilder Sınıfı

Giriş
Açıklaması şöyle.
In addition to steps a job configuration can contain other elements that help with parallelisation (<split/>), declarative flow control (<decision/>) and externalization of flow definitions (<flow/>).
Aslında step ile aynı şey. Sadece flow tanımı bir başka yerde de kullanılabilir. Açıklaması şöyle.
The effect of defining an external flow like this is simply to insert the steps from the external flow into the job as if they had been declared inline. In this way many jobs can refer to the same template flow and compose such templates into different logical flows. This is also a good way to separate the integration testing of the individual flows
build metodu
Örnek
Elimizde şöyle bir kod olsun
@Bean
protected Step step1(FlatFileItemReader reader, FileValidationTasklet 
  fileValidationTasklet) {
  return steps.get("step1").tasklet(fileValidationTasklet)
    .build();
}

@Bean
protected Step step2(FlatFileItemReader reader, EmployeeProcessor processor,
 EmployeeWriter writer) {
  return steps.get("step2")
    .<Employee, Employee>chunk(10)
    .reader(reader)
    .processor(processor)
    .writer(writer)
    .build();
}
Şöyle yaparız.
@Bean
public Job importEmployees(@Qualifier("step1") Step step1,
                           @Qualifier("step2") Step step2) {
  JobBuilder jobBuilder = jobBuilderFactory
    .get("importEmployees")
    .incrementer(new RunIdIncrementer());

  StepDecider fileDecider = new StepDecider();

  Flow flowFile = new FlowBuilder<Flow>("fileFlow")
    .start(fileDecider)
      .on(BatchConfig.STATE_DO_FILE_1)
        .to(step2)
      .on(BatchConfig.STATE_DO_FILE_2)
        .to(step2)
    .from(fileDecider)
      .on(BatchConfig.STATE_SKIP_FILE)
        .end(BatchConfig.STATE_FAILED)
      .on("*")
        .end(BatchConfig.STATE_COMPLETED)
    .build();

    /*
     * Complete Workflow:
     * 
     *           |--> (S2)
     *  S1 ---> -+
     *           |--> (S3)
     */
    FlowJobBuilder builder = jobBuilder
            .flow(step1)
            .next(flowFile)
            .end();

    return builder.build();
}
end metodu
Örnek
Şöyle yaparız.
@Configuration
@EnableBatchProcessing
public class BatchConfig {

  @Autowired
  private JobBuilderFactory jobBuilderFactory;

  @Autowired
  private StepBuilderFactory stepBuilderFactory;
  
  @Bean
  public Job job() throws Exception {
    return jobBuilderFactory.get("job1").flow(step1()).end().build();
  }

  @Bean
  public Step step1() throws Exception {
    return stepBuilderFactory.get("step1")
      .<BasicDBObject, BasicDBObject>chunk(10)
      .reader(reader())
      .writer(writer())
      .build();
  }

  @Bean
  public ItemReader<BasicDBObject> reader() {
    MongoItemReader<BasicDBObject> reader = new MongoItemReader<BasicDBObject>();
    ...
    return reader;
  }

  public ItemWriter<BasicDBObject> writer() {
    MongoItemWriter<BasicDBObject> writer = new MongoItemWriter<BasicDBObject>();
    ...
    return writer;
  }

}