반응형
목차
아래 포스팅에서 이어진 내용입니다.
[springBoot] spring batch preventRestart option
목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch scheduler modularization (feat. DB) 목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch scheduler jpaRead/Writer (feat. DB) 목차 아래 포
yaga.tistory.com
Step의 allowStartIfComplete옵션으로 이미 성공한 step도 실행시키는 예제
spring batch에서 3개의 스텝이 있을 경우 만일 3번에서 실패한 경우 job 재실행 시에는 기본적으로는
1,2번 스텝은 실행되지 않는다.(성공 했기 때문에)
그러나 저 옵션을 주면 성공한 스텝도 실행이 된다.(상황에 맞게 사용 가능할 듯)
JobTestConfig 😎
package com.dev.lsy.springbatchremind.batch;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.JobScope;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@RequiredArgsConstructor
@Configuration
public class JobTestConfig {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job job1() {
return jobBuilderFactory.get("job1")
.start(step1())
.next(step2())
.next(step3())
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.tasklet((a, b) -> {
log.info(" ======> job =====> [step1]");
return RepeatStatus.FINISHED;
})
.allowStartIfComplete(true)
.build();
}
@Bean
@JobScope
public Step step2() {
return stepBuilderFactory.get("step2")
.tasklet((a, b) -> {
log.info(" ======> [step1] =====> [step2]");
return RepeatStatus.FINISHED;
}).build();
}
@Bean
@JobScope
public Step step3() {
return stepBuilderFactory.get("step3")
.tasklet((a, b) -> {
// throw new RuntimeException("step3 was failed");
log.info(" ======> [step2] =====> [step3] [{}]");
return RepeatStatus.FINISHED;
}).build();
}
}
3개의 스텝을 만들고 3번 째에서 강제 예외 발생 시켜 3번 스텝을 실패 시킨 후 job 재실행 시 1번2번도 같이 실행되게 하는 예제
개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.
틀린 점 있을 경우 댓글 부탁드립니다.
reference: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B0%B0%EC%B9%98
반응형
'IT > development' 카테고리의 다른 글
[springBoot] spring batch step startLimit (54) | 2023.11.16 |
---|---|
[springBoot] spring batch JobScope, StepScope (55) | 2023.11.15 |
[springBoot] spring batch preventRestart option (50) | 2023.11.14 |
[springBoot] spring batch scheduler modularization (feat. DB) (49) | 2023.11.13 |
[springBoot] spring batch scheduler jpaRead/Writer (feat. DB) (54) | 2023.11.12 |