본문 바로가기
Framework/Spring-Batch

(4) Spring Batch 의존성 설정

by 조훙 2022. 11. 16.

Spring 배치 활성화

@EnableBatchProcessing

  • Spring Batch Annotation을 설정 할 경우 총 4개의 클래스를 실행시키며, 스프링 배치의 모든 초기화 및 실행 구성이 이루어짐
  • 스프링 부트 배치의 자동 설정 클래스가 실행됨으로 빈으로 등록된 모든 Job을 검색해서 초기화 동시에 Job을 수행하도록 구성됨

초기화 설정 클래스

BatchAutoConfiguration

  • 스프링 배치가 초기화 될 때 자동으로 실행되는 설정 클래스
  • Job 수행하는 JobLauncherApplicationRunner Bean 생성

SimpleBatchConfiguration

  • JobBuilterFactory 와 StepBuilderFactory 생성
  • 스프링 배치의 주요 구성 요소 생성 - 프록시 객체로 생성됨

BatchConfigureConfiguration

  • BasicBatchConfigurer
    • SimpleBatchConfiguration 에서 생성한 프록시 객체의 실제 대상 객체를 생성하는 설정 클래스
    • Bean으로 의존성 주입 받아서 주요 객체들을 참조해서 사용 할 수 있다.
    • BasicBatchConfigurer 를 상속받아 JPA 관련 객체를 생성하는 설정 클래스JpaBatchConfigurer

Sample Code

/*
# hellJobConfiguration.java
*/
@RequiredArgsConstructor
@Configuration
public class HelloJobConfiguration {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job helloJob() {
        return this.jobBuilderFactory.get("helloJob")
                .start(helloStep1())
                .next(helloStep2())
                .build();
    }

    @Bean
    public Step helloStep1() {
        return stepBuilderFactory.get("helloStep1")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        System.out.println(" ============================");
                        System.out.println(" >> Hello Spring Batch");
                        System.out.println(" ============================");
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }
    public Step helloStep2() {
        return stepBuilderFactory.get("helloStep2")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println(" ============================");
                    System.out.println(" >> Step2 has executed");
                    System.out.println(" ============================");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
}

Reference

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

(6) Spring Batch Step  (0) 2022.11.16
(5) Spring Batch Job  (0) 2022.11.16
(3) Spring-Batch Schema  (0) 2022.11.12
(2) Spring Batch Configuration  (0) 2022.11.12
(1) Spring Batch 란?  (0) 2022.11.11