스프링 스터디 (인프런)/스프링 배치 입문
배치 실행시 파라미터(파일 이름) 받기 및 (csv)검증
백엔드 개발자
2022. 12. 13. 23:16
package com.example.SpringBatchTutorial.ValidatedParam;
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.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.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
* desc : Hello World 출력
* run : --spring.batch.job.names=ValidatedParamJob
*/
@Configuration
@RequiredArgsConstructor
public class ValidatedParamJobConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job ValidatedParamJob(Step ValidatedParamStep) { //job 생성
return jobBuilderFactory.get("ValidatedParamJob")//이름을 정해주는 코드
.incrementer(new RunIdIncrementer())//아이디 부여시 시퀀스를 순차적으로 부여할 수 있도록 명시
.start(ValidatedParamStep)
.build();
}
@JobScope
@Bean
public Step ValidatedParamStep(Tasklet ValidatedParamTasklet) {
return stepBuilderFactory.get("ValidatedParamStep")
.tasklet(ValidatedParamTasklet) // step안에는 itemreader, processer, writer가 있는데, 읽고 쓰기를 하지 않는 단순한 배치를 만들 때 사용
.build();
}
@StepScope
@Bean
public Tasklet ValidatedParamTasklet() {
return new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("validated Param Tasklet");
return RepeatStatus.FINISHED;
}
};
}
}
이번에는 빈을 주입받아서 job을 실행해보았다.
함수를 호출하는 것이 아니라 파라미터로 각각 ValidatedParamStep, ValidatedParamTasklet을 받아서 호출한다.
결과적으로