1. spring batch 시작

후하후핳·2022년 5월 23일
0

spring batch

목록 보기
1/2

@RequiredArgsConstructor //자동으로 의존성 주입
@Configuration //하나의 배치 잡을 정의하고 빈 설정
public class HelloJobConfiguration {

    private final JobBuilderFactory jobBuilderFactory; //job을 생성하는 빌더 팩토리
    private final StepBuilderFactory stepBuilderFactory; //step을 생성하는 빌더 팩토리

    @Bean
    public Job helloJob() {
        //helloJob 이름으로 잡 생성
        return jobBuilderFactory.get("helloJob")
                .start(helloStep1())
                .next(helloStep2())
                .build();
    }
    @Bean
    public Step helloStep2() {
        //helloStep2 이름으로 Step 생성
        return stepBuilderFactory.get("helloStep2")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        //step안에서 단일 테스트로 수행하되는 로직 구현현                        System.out.println("-------------------");
                        System.out.println("-----step 2 !!-----");
                        System.out.println("-------------------");

                        //디폴트는 테스크릿은 무한 반복이다
                        //null과 RepeatStatus.FINISHED 한번만 실행
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }

    @Bean
    public Step helloStep1() {
        return stepBuilderFactory.get("helloStep1")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                        System.out.println("------------------------");
                        System.out.println("--hello spring batch!!--");
                        System.out.println("------------------------");

                        //디폴트는 테스크릿은 무한 반복이다
                        //null과 RepeatStatus.FINISHED 한번만 실행
                        return RepeatStatus.FINISHED;
                    }
                })
                .build();
    }
}
2022-05-23 23:49:06.199  INFO 49912 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=helloJob]] launched with the following parameters: [{}]
2022-05-23 23:49:06.211  INFO 49912 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [helloStep1]
------------------------
--hello spring batch!!--
------------------------
2022-05-23 23:49:06.217  INFO 49912 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [helloStep1] executed in 6ms
2022-05-23 23:49:06.218  INFO 49912 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [helloStep2]
-------------------
-----step 2 !!-----
-------------------

tasklet안에 비지니스 로직을 구현하면 된다.

*h2 의존성 추가하기

0개의 댓글