본문 바로가기
Spring|Spring-boot/Spring Batch

Batch 예제

by oncerun 2023. 5. 24.
반응형

 

최근 Spring Batch에서 제공해 주는 itemWriter와 itemReader, itemProcessor를 통해 chunk 단위의 배치를 연습하는 과정을 진행했다.

 

https://github.com/sungil-yu/exampleBatch

 

GitHub - sungil-yu/exampleBatch: Initial Commit

Initial Commit. Contribute to sungil-yu/exampleBatch development by creating an account on GitHub.

github.com

 

이제 csv 파일을 읽고 processor를 필터로 사용하고, 아웃풋 데이터를 db에 저장하고 로그를 찍는 배치를 개발해 본다.

 

 

1. Job을 Bean으로 등록한다.

 

2. Step을 생성하고 chunk 단위로 지정한다. 

 

3. 파일을 읽기 위해 Spring Batch가 제공하는 flatFileItemReader을 정의한다.

 

4. 필터링을 위해 itemProcessor을 생성한다. 나는 간단하게 처리하지만 책임 분리와 실제로는 ItemProcessor를 구현한 별도의 클래스를 만들어 활용하는 것이 좋다.

 

5. jdbcBatchItemWriter 혹은 jpaItemWriter를 사용하여 정의하고 이를 CompositeItemWriter로 합쳐 하나의 writer처럼 동작하게 한다. 이때 순서에 유의한다.

 

https://github.com/sungil-yu/exampleBatch/commit/32bdc55a608f97ca02858babf59de1cffb8cde10

 

homework · sungil-yu/exampleBatch@32bdc55

Show file tree Showing 3 changed files with 151 additions and 21 deletions.

github.com

 

 

이후에는 테스트 코드를 어떻게 작성하면 좋을지와 후처리, 전처리 등등 Listener까지 사용해 보자.

반응형

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

Batch 성능 개선  (1) 2023.05.29
Spring Batch Test Code  (0) 2023.05.28
Spring Batch 아키텍처  (0) 2023.05.21
Spring Batch about 15 minutes  (0) 2023.05.14
Batch란  (1) 2023.05.14

댓글