728x90
I. 스프링 배치 초기 선언 어노테이션
@SpringBootApplication
@EnableBatchProcessing // 스프링 배치 초기 선언 어노테이션
public class SpringBatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchApplication.class, args);
}
}
@EnableBatchProcessing
총 4개의 설정 클래스를 실행, 스프링 배치의 모든 초기화 및 실행 구성이 이뤄짐.
스프링 부트 배치의 자동 설정 클래스가 실행됨으로 빈으로 등록된 모든 Job을 검색해서 초기화와 동시에 Job을 수행하도록 구성.
II. 스프링 배치 초기화 설정 클래스
1. SimpleBatchConfiguration
- JobBuilderFactory와 StepBuilderFactory 생성, Job과 Step 생성용
- 스프링 배치의 주요 구성 요소 생성 - 프록시 객체로 생성됨.
Q. 프록시 객체란 : 대리인 역활을 하는 객체, 누군가 대신해서 일을 처리해주는 대리인
A. Proxy(대리인) -> Target(대상)
2. BatchConfigurerConfiguration
a. BasicBatchConfigurer
- SimpleBatchConfiguration에서 생성한 프록시 객체의 실제 대상 객체를 설정하는 설정 클래스
- Bean으로 의존성 주입 받아서 주요 객체들을 참조해서 사용할 수 있음.
b. JpaBatchConfigurer
- JPA 관련 객체를 생성하는 설정 클래스
- 사용자 정의 BatchConfigurer 인터페이스를 사용할 수 있음.
3. BatchAutoConfiguration
- 스프링 배치가 초기화 될 때 자동으로 실행되는 설정 클래스
- Job을 수행하는 JobLauncherApplicationRunner 빈을 생성
III. 스프링 배치 Job 기본 구성
package io.springbatch.springbatchseulgae;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@RequiredArgsConstructor // @Autowired 의존성 주입
@Configuration // 하나의 배치 Job을 정의하고 빈 설정
public class HelloJobConfiguration {
private final JobBuilderFactory jobBuilderFactory; // Job을 생성하는 빌더 팩토리
private final StepBuilderFactory stepBuilderFactory; // Step을 생성하는 빌더 팩토리
@Bean
public Job helloJob() { // helloJob 이름으로 Job 생성
return jobBuilderFactory.get("hello Job")
.start(helloStep1())
.next(helloStep2())
.build();
}
@Bean
public Step helloStep1() { // helloStep 이름으로 Step생성
return stepBuilderFactory.get("helloStep1")
.tasklet(new Tasklet() { // Step 안에서 단일 태스크로 수행되는 로직 구현
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
// 내용기술
System.out.println("Step1 테스트");
// Step 안 Tasklet은 무한 반복됨.
// 그래서 return 문에
// RepeatStatus을 사용해서 종료처리할 수 있음.
return RepeatStatus.FINISHED;
}
})
.build()
;
}
@Bean
public Step helloStep2() {
return stepBuilderFactory.get("helloStep1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
// 내용기술
System.out.println("Step2 테스트");
return RepeatStatus.FINISHED;
}
})
.build()
;
}
// 최종 동작 : Job 구동 -> Step을 실행 -> Teskelt을 실행
}
728x90
'Java & Spring > SpringBatch' 카테고리의 다른 글
| 스프링 배치 도메인 이해 - Job (1) | 2025.04.21 |
|---|---|
| Spring Batch 시작 - DB 스키마 생성(2) (0) | 2025.04.20 |
| Spring Batch 시작 - DB 스키마 생성(1) (1) | 2025.04.20 |
| Spring Batch 개요 (0) | 2025.04.19 |