관리 메뉴

IT.FARMER

Spring Batch chunk 지향 방식 본문

Spring/Spring Batch

Spring Batch chunk 지향 방식

아이티.파머 2020. 5. 26. 17:20
반응형

2020/05/26 - [Spring/Spring Batch] - Spring Batch 특징

2020/05/26 - [Spring/Spring Batch] - Spring batch 기본 흐름 (tasklet, next flow)

2020/05/26 - [Spring/Spring Batch] - Spring Batch 분기/decide

2020/05/26 - [Spring/Spring Batch] - Spring Batch JobParameter

2020/05/26 - [Spring/Spring Batch] - Spring Batch chunk 지향 방식

 

 

스프링 배치에서는 가장 일반적인 구현으로 청크지향 처리를 사용한다. 청크 지향 처리는 한번에 하나씩 데이터를 읽고 트랜젝션 경계 내애서 작성된 청크를 작성하는것을 의미 한다.

하나의 항목을 ItemReader 에서 읽고 ItemProcessor로 전달하고 집계 한다. 읽은 항목수가 커밋 간격과 같으면 청크 크기에 따라 ItemWriter 로 커밋된다.

 

 

다음 코드는 동일한 개념을 보여준다.

 

List items = new Arraylist();
for(int i = 0; i < commitInterval; i++){
    Object item = itemReader.read()
    Object processedItem = itemProcessor.process(item);
    items.add(processedItem);
}
itemWriter.write(items);

 

JavaConfig 작성 예제

/**
 * Note the JobRepository is typically autowired in and not needed to be explicitly
 * configured
 */
@Bean
public Job sampleJob(JobRepository jobRepository, Step sampleStep) {
    return this.jobBuilderFactory.get("sampleJob")
    			.repository(jobRepository)
                .start(sampleStep)
                .build();
}

/**
 * Note the TransactionManager is typically autowired in and not needed to be explicitly
 * configured
 */
@Bean
public Step sampleStep(PlatformTransactionManager transactionManager) {
	return this.stepBuilderFactory.get("sampleStep")
				.transactionManager(transactionManager)
				.<String, String>chunk(10)
				.reader(itemReader())
				.writer(itemWriter())
				.build();
}

 

  • reader : ItemReader 에서 처리할 항목을 제공함
  • writer : ItemReader 에서 자공하는 항목을 처리하는 ItemWriter
  • transactionManager : 처리중 트랜젝션을 시작하고 커밋하는 Spring의 Platform Transaction Manager
  • repository : 처리하는동안 (커밋직전) StepExecution 및 Execution Context를 주기적으로 저장하는 JobRepository
  • chunk : 항목 기반 단계이며 트랜잭션이 커밋 되기 전에 처리될 항목 수를 나타 낸다.
반응형

'Spring > Spring Batch' 카테고리의 다른 글

Spring Batch JobParameter  (0) 2020.05.26
Spring Batch 분기/decide  (0) 2020.05.26
Spring batch 기본 흐름 (tasklet, next flow)  (0) 2020.05.26
Spring Batch 특징  (0) 2020.05.26