IT/Live Coding

[springBoot] spring batch step startLimit(테스트 영상 & 소스코드 포함)

알 수 없는 사용자 2023. 11. 16.

아래 포스팅에서 이어진 내용입니다.

 

[springBoot] spring batch JobScope, StepScope

목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch allowStartIfComplete 목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch preventRestart option 목차 아래 포스팅에서 이어진 내용

yaga.tistory.com


실패한 job의 경우에도 startLimit 옵션을 줘서 재시작 횟수 제한을 하는 예제

JobConfig 🙂

package com.dev.lsy.springbatchremind.batch;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.ExitStatus;
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.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Value;
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())
                .build();
    }

    @Bean
    public Step step1() {

        log.info("============= > step1");

        return stepBuilderFactory.get("step1")
                .tasklet(tasklet1())
                //횟수 제한		
                .startLimit(2)
                .build();
    }

    @Bean
    public Tasklet tasklet1() {
        return ((contribution, chunkContext) -> {

            log.info("============= > taklet1 ");

            throw new RuntimeException("강제 예외");
//            return RepeatStatus.FINISHED;
        });
    }
}

개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.

틀린 점 있을 경우 댓글 부탁드립니다.

reference: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B0%B0%EC%B9%98


다음 내용

 

[springBoot] spring batch FlatFileItemWriter

목차 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch step startLimit 아래 포스팅에서 이어진 내용입니다. [springBoot] spring batch JobScope, StepScope 목차 아래 포스팅에서 이어진 내용입니다. [spr

yaga.tistory.com

 

댓글