0JUUU / spring-batch

Spring Boot 기반으로 개발하는 Spring Batch
1 stars 0 forks source link

섹션 4. 스프링 배치 실행 - Job #8

Open 0JUUU opened 2 years ago

0JUUU commented 2 years ago
0JUUU commented 2 years ago

배치 초기화 설정

많이 사용하는 설정들을 알아보자

spring:
  batch:
    job:
      names: batchJob1 # 이렇게 하드코딩해서 실행시키고자 하는 Job을 설정할 수 있음

만약 없는 Job 이름을 설정한다면 ❓

image

있는 Job으로 설정한다면 ❓

image

디버깅 🐞

image

테이블 prefix 변경

spring:
  batch:
    jdbc:
      table-prefix: SYSTEM_ # default : BATCH_
image

여러 개의 Job을 실행시켜보자.

// 다른 Job 하나 더 생성
@RequiredArgsConstructor
@Configuration
public class JobConfiguration2 {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job batchJob2() {
        return this.jobBuilderFactory.get("batchJob2")
                                     .incrementer(new RunIdIncrementer())
                                     .start(step3())
                                     .next(step4())
                                     .build();
    }

    @Bean
    public Step step3() {
        return this.stepBuilderFactory.get("step3")
                                      .tasklet(new Tasklet() {
                                          @Override
                                          public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                                              System.out.println(">> step3 has executed");
                                              return RepeatStatus.FINISHED;

                                          }
                                      })
                                      .build();
    }

    @Bean
    public Step step4() {
        return this.stepBuilderFactory.get("step4")
                                      .tasklet(((contribution, chunkContext) -> {
                                          System.out.println(">> step4 has executed");
                                          return RepeatStatus.FINISHED;
                                      }))
                                      .build();

    }
}
image image
0JUUU commented 2 years ago

JobBuilderFactory

JobBuilderFactory

JobBuilder

아키텍처

image

상속 구조

image

0JUUU commented 2 years ago

SimpleJob - 개념 및 API 소개

@Bean
public Step step3() {
    return stepBuilderFactory.get("step3")
                .tasklet((contribution, chunkContext) -> {
                         chunkContext.getStepContext().getStepExecution().setStatus(BatchStatus.FAILED);
                         contribution.setExitStatus(ExitStatus.STOPPED);
                         System.out.println(">> step3 has executed");
                         return RepeatStatus.FINISHED;
                })
               .build();
}

BATCH_STEP_EXECUTION

image

BATCH_JOB_EXECUTION

image
0JUUU commented 2 years ago

SimpleJob - start() / next()

0JUUU commented 2 years ago

SimpleJob - validator()

첫 번째 체크 단계. SimpleJobLauncher

image

두 번째 체크 단계. SimpleJob(실제는 AbstractJob에 구현)

image

DefaultJobParametersValidator 사용

0JUUU commented 2 years ago

SimpleJob - preventRestart()

JobBuilderHelper

image
public abstract class JobBuilderHelper<B extends JobBuilderHelper<B>> {
    public B preventRestart() {
        properties.restartable = false;
        @SuppressWarnings("unchecked")
        B result = (B) this;
        return result;
    }
}

의도적인 에러 발생

image

BATCH_JOB_EXECUTION

image
0JUUU commented 2 years ago

SimpleJob - incrementer()

커스텀해서

public class CustomJobParametersIncrementer implements JobParametersIncrementer {

    static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-hhmmss");
    @Override
    public JobParameters getNext(JobParameters parameters) {
        String id = format.format(new Date());
        return new JobParametersBuilder().addString("run.id", id).toJobParameters();
    }
}
    @Bean
    public Job batchJob() {
        return this.jobBuilderFactory.get("batchJob")
                                     .start(step1())
                                     .next(step2())
                                     .incrementer(new CustomJobParametersIncrementer())
                                     .build();
    }
image

Spring Batch 에서 기본 제공하는 기능 : RunIdIncremeter

.incrementer(new RunIdIncrementer())
0JUUU commented 2 years ago

SimpleJob 아키텍처

image