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)의 실행
대용량 데이터를 알아서 처리한다는 점에서 아주 매력적인 프레임워크라고 볼 수 있다.
공식문서를 이용하여 Spring Batch에 대하여 자세히 알아보자.
[참고 자료] https://docs.spring.io/spring-batch/reference/index.html
Overview :: Spring Batch
The reference documentation is divided into several sections:
docs.spring.io
Spring Batch Architecture | 참고
Spring Batch는 Application, Batch Core, Batch Infrastructure이라는 3가지 Layer로 구성되어 있다.
Application | batch jobs와 개발자가 작성한 커스텀 코드를 담고 있는 부분 |
Batch Core |
batch jobs를 수행/제어하는데 필요한 core runtime classes를 담고 있는 부분 |
JobLauncher, Job, Step의 구현체를 포함함 | |
Batch Infrastructure | common readers, writers, services를 담고 있는 부분 |
Spring Batch 구성 요소 | 참고
- 각 Job은 1개 이상의 Step을 가짐
- 각 Step은 하나의 ItemReader, ItemProcessor, ItemWriter를 가짐
- 프로세스에 필요한 metadata는 JobRepository에 저장
1. Job
A Job is an entity that encapsulates an entire batch process.
In Spring Batch, a Job is simply a container for Step instances.
Job은 batch process를 감싸는 단위로, 여러 개의 step을 포함하고 있다.
Job Configuration을 설정하기 위해서는 아래 3가지 요소를 함께 정의해주어야 한다.
- The name of the Job.
- Definition & Ordering of STEP instances.
- Whether or Not the job is restartable.
/* 예제 */
@Bean
public Job footballJob(JobRepository jobRepository) {
return new JobBuilder("footballJob", jobRepository)
.start(playerLoad())
.next(gameLoad())
.next(playerSummarization())
.build();
}
1-1. JobInstance
A JobInstance refers to the concept of a logical job run.
JobInstance는 job의 실행을 논리적으로 나타내는 개념이다.
여러 개의 JobExecution들로 구성되어 있으며, JobParameter를 기준으로 구분된다.
만약 매일 실행되는 작업 A가 있을 때, 10일에 수행이 실패했던 작업이 11일에 다시 수행된다고 해보자. 이때, 11일에는 2가지 작업 A1, A2가 수행될 것이다. A1은 10일에 수행이 실패하여 다시 진행되는 작업이며, A2는 원래 11일에 수행 예정이었던 작업이다. A1과 A2는 동일한 작업을 수행하더라도 서로 다른 객체처럼 구분된다. 이러한 작업 객체를 JobInstance라고 한다고 이해하면 된다.
1-2. JobParameter
JobInstance를 구분하기 위한 식별자이다.
1-3. JobExecution
A JobExecution refers to the technical concept of a single attempt to run a Job.
JobInstance를 구성하고 있는 '실행 시도' 단위이다.
만약 동일한 JobInstance에서 작업이 실패하여 다음날 다시 수행한다고 하면, 이는 동일한 JobInstance 내에서 새로운 JobExecution이 발생한 것이지, 새로운 JobInstance가 생성된 것은 아니다.
JobExecution은 아래와 같은 다양한 properties를 갖는다.
Property | Definition |
status | Execution의 status를 나타냄 - STARTED : Running - FAILED : Failed - COMPLETED : Successfully finished |
startTime | current system time when the execution was started |
endTime | current system time when the execution finished, regardless of whether or not it was successful |
... | ... |
2. Step
A Step is a domain object that encapsulates an independent, sequential phase of a batch job.
Step은 하나의 Job에서 발생하는 독립적 & 순차적 작업 건수를 의미한다.
각 Step은 여러 개의 StepExecution으로 구성되어 있다.
StepExecution
A StepExecution represents a single attempt to execute a Step.
StepExecution은 JobExecution과 유사한 개념으로, Step을 수행하는 '실행 시도' 단위이다.
Step이 실제로 시작될 때 생성된다는 특징이 있다.
각 StepExecution은 ExecutionContext를 갖는다.
이는 batch가 실행될 때 개발자가 필요한 데이터를 저장하는 논리적 공간을 의미한다.
StepExecution은 JobExecution처럼 아래와 같은 properties를 가질 수 있다.
Property | Definition |
status | Execution의 status를 나타냄 - STARTED : Running - FAILED : Failed - COMPLETED : Successfully finished |
startTime | current system time when the execution was started |
endTime | current system time when the execution finished, regardless of whether or not it was successful |
... | ... |
3. ExecutionContext
An ExecutionContext represents a collection of key/value pairs that are persisted and controlled by the framework to give developers a place to store persistent state that is scoped to a StepExecution object or a JobExecution object. (For those familiar with Quartz, it is very similar to JobDataMap.)
ExecutionContext는 key-value로 이루어진 컬렉션으로,
JobExecution이나 StepExecution 수행 과정에서 저쟝해야 할 데이터/상태를 저장하고 관리하는 역할을 한다.
'Spring > Spring Batch' 카테고리의 다른 글
[Spring Batch] Batch 활용 목적과 Performance 개선 방법 (0) | 2024.01.24 |
---|---|
[Spring Batch] Spring Batch, Quartz 이해하기 (0) | 2024.01.07 |