13 Aralık 2018 Perşembe

SpringBatch StepBuilder Sınıfı

Giriş
Şu satırı dahil ederiz
import org.springframework.batch.core.step.builder.StepBuilder;
Step nesnesi iki şekilde çalışabilir.
1. chunk : Çok sayıda satırı, nesneyi, girdiyi bir anda işler
2. tasklet : Tek bir iş üzerinde çalışır

chunk metodu
Açıklaması şöyle.
The chunk() method builds a step that processes items in chunks with the size provided, with each chunk then being passed to the specified reader, processor, and writer.
Örnek
Şöyle yaparız.
@Configuration
public class JobConfiguration {

  @Autowired
  private StepBuilderFactory stepBuilderFactory;

  
  @Bean
  public JdbcPagingItemReader<Customer> customerPagingItemReader(){
    ...
  }

  @Bean
  public StaxEventItemWriter<Customer> customerItemWriter() throws Exception{
    ...
  }


  @Bean
  public Step step1() throws Exception {
    return stepBuilderFactory.get("step1")
      .<Customer, Customer>chunk(100)
      .reader(customerPagingItemReader())
      .writer(customerItemWriter())
      .build();
  }
  ...
  
}
Örnek
Elimizde bir reader olsun.
@Bean
public JdbcCursorItemReader<User> reader() {
  JdbcCursorItemReader<User> reader = new JdbcCursorItemReader<User>();
  reader.setDataSource(dataSource);
  reader.setSql("SELECT id,name FROM user");
  reader.setRowMapper(new UserRowMapper());
  return reader;
}

public class UserRowMapper implements RowMapper<User> {

  @Override
  public User mapRow(ResultSet rs, int rowNum) throws SQLException {
    User user = new User();
    user.setId(rs.getInt("id"));
    user.setName(rs.getString("name"));

    return user;
  }

}
Elimizde bir processor olsun.
@Bean
public UserItemProcessor processor() {
  return new UserItemProcessor();
}
Elimizde bir writer olsun.
@Bean
public FlatFileItemWriter<User> writer() {
  FlatFileItemWriter<User> writer = new FlatFileItemWriter<User>();
  writer.setResource(new ClassPathResource("users.csv"));
  writer.setLineAggregator(new DelimitedLineAggregator<User>() {
    {
      setDelimiter(",");
      setFieldExtractor(new BeanWrapperFieldExtractor<User>() {
        {
          setNames(new String[] { "id", "name" });
        }
      });
    }
  });

  return writer;
}
Şöyle yaparız.
@Bean
public Step step1() {
  return stepBuilderFactory.get("step1")
    .<User, User>chunk(10)
    .reader(reader())
    .processor(processor())
    .writer(writer())
    .build();
}
chunk metodu - TransactionManager
Örnek
Şöyle yaparız
// step that load the content of the csv with titlecsvReader
// wrote them via titleItemWriter
@Bean 
public Step loadCsv(JobRepository jobRepository, 
                    PlatformTransactionManager transactionManager, 
                    FlatFileItemReader<Title> titleCsvReader){
  
return new StepBuilder("load csv",jobRepository)
  .<Title,Title>chunk(10,transactionManager)
  .reader(titleCsvReader)
  .writer(titleItemWriter())
  .build();
}
faultTolerant metodu
SpringBatch StepBuilder Dealing With Errors yazısına taşıdım

listener metodu

retryLimit metodu

Hiç yorum yok:

Yorum Gönder