0JUUU / spring-batch

Spring Boot 기반으로 개발하는 Spring Batch
1 stars 0 forks source link

섹션 5. 스프링 배치 실행 - Step #9

Open 0JUUU opened 2 years ago

0JUUU commented 2 years ago
0JUUU commented 2 years ago

StepBuilderFactory

StepBuilderFactory

상속 구조

image

0JUUU commented 2 years ago

TaskletStep - 개념 및 API 소개

Task vs Chunk

사용 방법

image

0JUUU commented 2 years ago

TaskletStep - tasklet()

Tasklet

0JUUU commented 2 years ago

TaskletStep - startLimit() / allowStartIfComplete()

startLimit()

startLimit 확인

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2")
            .tasklet(new Tasklet() {
                @Override
                public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                    System.out.println("stepContribution = " + stepContribution + ", chunkContext = " + chunkContext);
                    throw new RuntimeException("");
//                        return RepeatStatus.FINISHED;
                }
            })
            .startLimit(3)
            .build();
    }

처음 실행 - BATCH_STEP_EXECUTION

image

Step2 3번 이상 실행

image

allowStartIfComplete(true)

BATCH_STEP_EXECUTION

image
0JUUU commented 2 years ago

TaskletStep 아키텍처

image

image

0JUUU commented 2 years ago

JobStep

// 실습코드
@RequiredArgsConstructor
@Configuration
public class JobStepConfiguration {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job parentJob() {
        return this.jobBuilderFactory.get("parentJob")
                                     .start(jobStep(null))
                                     .next(step2())
                                     .build();
    }

    @Bean
    public Step jobStep(JobLauncher jobLauncher) {
        return stepBuilderFactory.get("jobStep")
            .job(childJob())
            .launcher(jobLauncher)
            .parametersExtractor(jobParametersExtractor())
            .listener(new StepExecutionListener() {
                @Override
                public void beforeStep(StepExecution stepExecution) {
                    stepExecution.getExecutionContext().putString("name", "user1");
                }

                @Override
                public ExitStatus afterStep(StepExecution stepExecution) {
                    return null;
                }
            })
            .build();
    }

    @Bean
    public Job childJob() {
        return this.jobBuilderFactory.get("childJob")
                                     .start(step1())
                                     .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                                 .tasklet(new Tasklet() {
                                     @Override
                                     public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                                        return RepeatStatus.FINISHED;
                                     }
                                 })
                                 .build();
    }

    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step2")
                                 .tasklet(new Tasklet() {
                                     @Override
                                     public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        return RepeatStatus.FINISHED;
                                     }
                                 })
                                 .build();
    }

    private DefaultJobParametersExtractor jobParametersExtractor() {
        DefaultJobParametersExtractor extractor = new DefaultJobParametersExtractor();
        extractor.setKeys(new String[]{"name"});
        return extractor;
    }
}

BATCH_JOB_INSTANCE

image

BATCH_JOB_EXECUTION

image

BATCH_JOB_EXEUCTION_PARAMS

image

BATCH_STEP_EXECUTION

image

Step1에 의도적인 에러

BATCH_JOB_EXECUTION

image

BATCH_STEP_EXECUTION

image

Step2를 실패

BATCH_JOB_EXECUTION

image

BATCH_STEP_EXECUTION

image