[SpringBatch 실습] 10. STEP의 EXIT_CODE, STATUS 설정하기

반응형
728x90
반응형

Job 생성

import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
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=simpleJob
 */

@Configuration
@RequiredArgsConstructor
public class SimpleJobConfiguration {

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

    @Bean
    public Job simpleJob() {
        return this.jobBuilderFactory.get("simpleJob")
                /* step start */
                .start(simpleStep1())
                .next(simpleStep2())
                .incrementer(new RunIdIncrementer())
                .build();
    }

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

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

 

STEP2 에서 STATUS를 설정해보자.

chunkContext.getStepContext().getStepExecution().setStatus(BatchStatus.FAILED);

 

STEP2에서 EXIT_CODE를 설정해보자.

contribution.setExitStatus(ExitStatus.STOPPED); // 종료코드는 STOPPED

 

수정된 Step2 메서드 전체 로직
@Bean
public Step simpleStep2() {
    return stepBuilderFactory.get("simpleStep2")
            .tasklet(new Tasklet() {
                @Override
                public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                    System.out.println("simpleStep2");

                    chunkContext.getStepContext().getStepExecution().setStatus(BatchStatus.FAILED);
                    contribution.setExitStatus(ExitStatus.STOPPED); // 종료코드는 STOPPED

                    return RepeatStatus.FINISHED;
                }
            })
            .build();
}

 

 

배치 수행

결과
validatorStep1
validatorStep2

 

 

DB 테이블 조회

1) BATCH_JOB_EXECUTION

COLUMN VALUE
STATUS FAILED
EXIT_CODE STOPPED
JOB_EXECUTION_ID 2

 

2) BATCH_JOB_INSTANCE

COLUMN VALUE
JOB_NAME simpleJob

 

3) BATCH_STEP_EXECUTION

  • simpleStep1
COLUMN VALUE
STEP_NAME simpleStep1
EXIT_CODE COMPLETED
STATUS COMPLETED
JOB_EXECUTION_ID 3

 

  • simpleStep2
COLUMN VALUE
STEP_NAME simpleStep2
EXIT_CODE STOPPED
STATUS FAILED
JOB_EXECUTION_ID 3

 

 

알수있는점

BATCH_JOB_EXECUTION의 STATUS, EXIT_CODE는 최종적으로 실행된 STEP의 상태값이 반영된다.

 

 

 

반응형

Designed by JB FACTORY