Spring Batch
[SpringBatch 실습] 15. STEP이 COMPELETED 상태로 완료되어도 재실행에 포함시키기 (allowStartIfCompleted(true)), STEP별 재실행 가능 횟수 설정하기 (startLimit())
LearnerKSH
2022. 6. 1. 11:03
728x90
반응형
재실행에 포함시키기 - allowStartIfCompleted(true)
@Bean
public Step limitAllowStepStep1() {
return stepBuilderFactory.get("limitAllowStepStep1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("limitAllowStepStep1");
return RepeatStatus.FINISHED;
}
})
.allowStartIfComplete(true) // COMPLETED 되도 재실행에 포함
.build();
}
재실행 가능 횟수 설정 - startLimit()
@Bean
public Step limitAllowStepStep2() {
return stepBuilderFactory.get("limitAllowStepStep2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("limitAllowStepStep2");
throw new RuntimeException("step2 was failed");
//return RepeatStatus.FINISHED;
}
})
.startLimit(2) // 해당 스텝의 실행은 2번까지만 가능, 3번부터 초과되어 오류 발생
.build();
}
Job 생성
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;
/*
--job.name=limitAllowStepJob
*/
@Configuration
@RequiredArgsConstructor
public class Limit_AllowConfiguration {
// job 생성
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job limitAllowStepJob() {
return this.jobBuilderFactory.get("limitAllowStepJob")
.start(limitAllowStepStep1())// COMPLETED (재시작에 포함되지 않음) -> allowStartIfComplete 추가 후 포함됨
.next(limitAllowStepStep2())// FAILED (재시작의 대상)
.build();
}
@Bean
public Step limitAllowStepStep1() {
return stepBuilderFactory.get("limitAllowStepStep1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("limitAllowStepStep1");
return RepeatStatus.FINISHED;
}
})
.allowStartIfComplete(true) // COMPLETED 되도 재실행에 포함
.build();
}
@Bean
public Step limitAllowStepStep2() {
return stepBuilderFactory.get("limitAllowStepStep2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("limitAllowStepStep2");
throw new RuntimeException("step2 was failed");
//return RepeatStatus.FINISHED;
}
})
.startLimit(2)
.build();
}
}
1) .allowStartIfComplete(true)
limitAllowStepStep1 스텝이 COMPLETED 상태로 완료되었어도 다음 Job 재실행때 포함시킨다.
2) startLimit(2)
해당 스텝의 실행은 2번까지만 가능하고, 3번부터 초과되어 오류 발생한다.
Job 실행
- 첫번째 실행 (정상)
limitAllowStepStep1
limitAllowStepStep2
java.lang.RuntimeException: step2 was failed
- 두번째 실행 (정상)
위 첫번째 실행에서 limitAllowStepStep1이 정상적으로 완료되었지만 재수행때 포함되어 함께 실행된다.
만약 allowStartIfComplete(true) 설정을 주석처리한 후 재수행한다면 limitAllowStepStep1은 재수행되지 않는다.
limitAllowStepStep1
limitAllowStepStep2
java.lang.RuntimeException: step2 was failed
- 세번째 실행 (오류 발생 - startLimit(2) 에서 2를 초과했기 때문)
limitAllowStepStep1은 정상적으로 수행되었고, limitAllowStepStep2 수행시 StartLimitExceededException 에러가 발생한다.
limitAllowStepStep1
org.springframework.batch.core.StartLimitExceededException:
Maximum start limit exceeded for step: limitAllowStepStep2StartMax: 2
DB 테이블 조회
1) BATCH_JOB_EXECUTION
| COLUMN | 첫번째 수행 | 두번째 수행 | 세번째 수행 |
| STATUS | FAILED | FAILED | FAILED |
| EXIT_CODE | FAILED | FAILED | FAILED |
| JOB_EXECUTION_ID | 19 | 20 | 21 |
2) BATCH_JOB_INSTANCE
| COLUMN | VALUE |
| JOB_NAME | limitAllowStepJob |
| JON_INSTANCE_ID | 15 |
3) BATCH_STEP_EXECUTION
- limitAllowStepStep1
| COLUMN | 첫번째 수행 | 두번째 수행 | 세번째 수행 |
| EXIT_CODE | COMPLETED | COMPLETED | COMPLETED |
| STATUS | COMPLETED | COMPLETED | COMPLETED |
| JOB_EXECUTION_ID | 19 | 20 | 21 |
- limitAllowStepStep2
| COLUMN | 첫번째 수행 | 두번째 수행 | 세번째 수행 |
| EXIT_CODE | FAILED | FAILED | StartLimitExceededException 오류 발생으로 실행되지 않음 |
| STATUS | FAILED | FAILED | |
| JOB_EXECUTION_ID | 19 | 20 |
반응형