본문 바로가기
Spring|Spring-boot/Spring Batch

Spring Batch step 작성해보기

by oncerun 2021. 2. 27.
반응형

job에서 여러 가지 step을 만들기 연습이다.

 

 


    @Bean
    public Step shareStep(){
        return stepBuilderFactory.get("shareStep")
                .tasklet((contribution, chunkContext) ->{
                            StepExecution stepExecution = contribution.getStepExecution();
                            ExecutionContext stepExecutionContext = stepExecution.getExecutionContext();
                            stepExecutionContext.putString("stepKey", "step execution context");

                            JobExecution jobExecution = stepExecution.getJobExecution();
                            JobInstance jobInstance = jobExecution.getJobInstance();
                            ExecutionContext jobExecutionContext = jobExecution.getExecutionContext();
                            jobExecutionContext.putString("jobKey", "job execution context");
                            JobParameters jobParameters = jobExecution.getJobParameters();


                            log.info("jobName : {} , stepName : {} , parameter : {}",
                                    jobInstance.getJobName(),
                                    stepExecution.getStepName(),
                                    jobParameters.getLong("run.id"));

                            return RepeatStatus.FINISHED;


                }
                ).build();
    }


}

tasklet의 contribution -> stepExcution -> stepExecutionContext를 꺼낸다음. 공유 값을 넣었습니다.

stepExcution -> jobExecution -> jobInstance

jobExcution -> jobExecutionContext -> putString 공유값 넣기

jobExcution -> jobParameters 

 

 

@Configuration
@Slf4j
public class sharedConfigure {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;


    public sharedConfigure(JobBuilderFactory jobBuilderFactory,
                           StepBuilderFactory stepBuilderFactory){
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
    }



    @Bean
    public Job shareJob(){
        return jobBuilderFactory.get("shareJob")
                .incrementer(new RunIdIncrementer())
                .start(this.shareStep())
                .next(this.shareStep2())
                .build();
    }

    @Bean
    public Step shareStep2(){
        return stepBuilderFactory.get("shareStep2")
                .tasklet((contribution, chunkContext) -> {
                    StepExecution stepExecution = contribution.getStepExecution();
                    ExecutionContext stepExecutionContext = stepExecution.getExecutionContext();

                    JobExecution jobExecution = stepExecution.getJobExecution();
                    ExecutionContext jobExecutionContext = jobExecution.getExecutionContext();

                    log.info("jobKey : {}, stepKey : {}",
                            jobExecutionContext.getString("jobKey", "emptyJobKey"),
                            stepExecutionContext.getString("stepKey", "emptyStepKey"));

                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Step shareStep(){
        return stepBuilderFactory.get("shareStep")
                .tasklet((contribution, chunkContext) ->{
                            StepExecution stepExecution = contribution.getStepExecution();
                            ExecutionContext stepExecutionContext = stepExecution.getExecutionContext();
                            stepExecutionContext.putString("stepKey", "step execution context");

                            JobExecution jobExecution = stepExecution.getJobExecution();
                            JobInstance jobInstance = jobExecution.getJobInstance();
                            ExecutionContext jobExecutionContext = jobExecution.getExecutionContext();
                            jobExecutionContext.putString("jobKey", "job execution context");
                            JobParameters jobParameters = jobExecution.getJobParameters();


                            log.info("jobName : {} , stepName : {} , parameter : {}",
                                    jobInstance.getJobName(),
                                    stepExecution.getStepName(),
                                    jobParameters.getLong("run.id"));

                            return RepeatStatus.FINISHED;


                }
                ).build();
    }


}

내가 의도한 건 job실행시 step -> step 2 실행하고

 

stepExecutionContext에 넣은 내용이 step 끼리 공유가 되는가?

jobExecutionContext에 넣은 내용이 어디까지 공유가 되는가이다.

 

step에서 jobKey와 stepKey를 Context에 넣었고 step 2에서 jobKey와 stepKey를 꺼내서 로그를 출력했다.

보면 jobConText에 있는 내용을 공유가 되어있지만 Stepkey는 공유가 되지 않았다.

step은 한 스탭에서만 공유만 된다.

반응형

'Spring|Spring-boot > Spring Batch' 카테고리의 다른 글

Spring Batch Parameter  (0) 2021.02.28
Spring Batch Architecture  (0) 2021.02.27
Spring Batch Job Repository  (0) 2021.02.27
Batch Hello World  (0) 2021.02.27
Spring Batch  (0) 2021.02.27

댓글