[SpringBatch 실습] 20. JobExecutionDecider 구현체 생성해서 커스텀 값으로 from().on() 설정

반응형
728x90
반응형

Job 생성

JobExecutionDeciderConfiguration.java
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.job.flow.JobExecutionDecider;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
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;

/*
--job.name=jobExecutionDeciderJob
 */

@Configuration
@RequiredArgsConstructor
public class JobExecutionDeciderConfiguration {

    // job 생성
    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job jobExecutionDeciderJob() {
        return this.jobBuilderFactory.get("jobExecutionDeciderJob")
                .incrementer(new RunIdIncrementer()) // job 여러번 실행 가능
                .start(deciderEvenStartStep())
                .next(decider())
                /* SimpleFlow */
                .from(decider()).on("ODD").to(deciderOddStep())
                .from(decider()).on("EVEN").to(deciderEvenStep())
                .end()
                .build();
    }

    /**
     * Decider Class
     * @return
     */
    @Bean
    public JobExecutionDecider decider() {
        return new CustomDecider();
    }

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

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

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

1) Decider Class 설정

@Bean
public JobExecutionDecider decider() {
    return new CustomDecider();
}

 

2) 실행 흐름 설정

@Bean
public Job jobExecutionDeciderJob() {
    return this.jobBuilderFactory.get("jobExecutionDeciderJob")
            .incrementer(new RunIdIncrementer()) // job 여러번 실행 가능
            .start(deciderEvenStartStep())
            .next(decider())
            /* SimpleFlow */
            .from(decider()).on("ODD").to(deciderOddStep())
            .from(decider()).on("EVEN").to(deciderEvenStep())
            .end()
            .build();
}
Step parttern next Step
deciderEvenStartStep()    
decider() "ODD" deciderOddStep()
  "EVEN" deciderEvendStep()

 

CustomDecider.java
package com.project.springbatch._32_decider;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;

public class CustomDecider implements JobExecutionDecider {
    private int count = 0;

    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        count++;

        if (count % 2 == 0) { // 짝
            return new FlowExecutionStatus("EVEN");
        } else { // 능
            return new FlowExecutionStatus("ODD");
        }
    }
}

1) count 가 짝수일 경우 "EVEN", 홀수일 경우 "ODD" 

if (count % 2 == 0) { // 짝
    return new FlowExecutionStatus("EVEN");
} else { // 능
    return new FlowExecutionStatus("ODD");
}

 

 

DB 테이블 조회

1) BATCH_JOB_EXECUTION

COLUMN VALUE
STATUS COMPLETED
EXIT_CODE COMPLETED
JOB_EXECUTION_ID 23

 

2) BATCH_JOB_INSTANCE

COLUMN VALUE
JOB_NAME jobExecutionDeciderJob

 

3) BATCH_STEP_EXECUTION

  • deciderEvenStartStep
COLUMN VALUE
STEP_NAME deciderEvenStartStep
EXIT_CODE COMPLETED
STATUS COMPLETED
JOB_EXECUTION_ID 23

 

  • deciderOddStep

count: 1 이므로 deciderOddStep()이 수행되었다.

COLUMN VALUE
STEP_NAME deciderOddStep
EXIT_CODE COMPLETED
STATUS COMPLETED
JOB_EXECUTION_ID 23

 

 

반응형

Designed by JB FACTORY