본문 바로가기
Spring/Spring Batch

[Spring Batch] Spring Batch, Quartz 이해하기

by seoyamin 2024. 1. 7.

요구 사항

개발중인 서비스에서는  특정 시간에 프로그램 참여자 전체를 대상으로 대량의 정산 로직이 필요했다.
이를 보다 효율적으로 처리하기 위하여 Spring Batch를 이용하기로 결정하였다.

 

 

Overview_Spring Batch ?

The ability of batch processing to efficiently process large amounts of data makes it ideal for many use cases. Spring Batch’s implementation of industry-standard processing patterns lets you build robust batch jobs on the JVM.  | 공식 문서

 

Spring Batch란, 한마디로 일괄 처리 (batch processing)를 지원하는 프레임워크이다. 

* 일괄 처리 : 최종 사용자의 개입 없이 또는 (자원이 허가한다면) 실행을 스케줄링할 수 있는 작업(job)의 실행

 

대용량 데이터 알아서 처리한다는 점에서 아주 매력적인 프레임워크라고 볼 수 있다.

 

 

Overview_Quartz ?

Quartz is a richly featured, open source job scheduling library that can be integrated within virtually any Java application - from the smallest stand-alone application to the largest e-commerce system. [공식 문서]

 

Quartz는 Spring Batch와 함께 사용하기 좋은 Enterprise Scheduler이다. Java Application이 가지는 job들을 특정 시간이나 이벤트 조건을 통해 자동 실행하는 라이브러리라고 이해하면 된다.

 

Spring Batch는 [대량 데이터 일괄 처리]를, Quartz는 [특정 시간 조건에 따라 작업 수행]을 담당한다.

따라서 Quartz를 통해 Spring Batch를 트리거하는 것이다. 둘은 다른 개념임을 유의하자.

 

본인이 진행중인 프로젝트에서는 특정 시간에 맞추어 이메일을 대량 전송하는 로직이 필요했다.
따라서 시간과 관련하여 다양한 스케쥴링 기능을 제공하는 Quartz를 함께 사용하기로 결정하였다.

 

 


build.gradle

// batch
implementation 'org.springframework.boot:spring-boot-starter-batch'

 

 

schema-mysql.sql

MySQL 환경에서 Spring Batch를 사용하는 경우, metadata schema를 직접 수동으로 생성해주어야 한다.
Intellij에서 shift를 연속 2번 누르면 전체 파일을 검색할 수 있는 팝업이 뜨는데, 여기서 'schema-mysql.sql'을 검색하면 이미 자동으로 생성되어 있는 파일을 볼 수 있을 것이다. 이 sql 파일을 그대로 자신의 MySQL DB에서 실행해주면 된다.

본인은 MySQL WorkBench에서 실행했다.

 

실행 후, ERD를 추출해보면 아래와 같다.