반응형
728x90
반응형
해당 포스팅의 결론
여러개의 Tasklet을 선언할 경우 마지막에 선언된 Tasklet이 수행된다.
@Bean
public Step onlyTaskletStep2() {
return stepBuilderFactory.get("onlyTaskletStep2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("실행되면 안되요");
return RepeatStatus.FINISHED;
}
})
.tasklet(new OnlyTasklet()) // 여러개 불가능, 마지막 tasklet 실행
.build();
}
위 코드에 .tasklet() 메서드를 사용하여 2개의 tasklet을 설정했다.
이때 수행되는 tasklet()은 마지막에 선언된 OnlyTasklet 클래스의 execute 메서드가 수행된다.
Job 생성
OnlyTaskletStepConfiguration.java
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.StepBuilderFactory;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
--job.name=onlyTaskletJob
*/
@Configuration
@RequiredArgsConstructor
public class OnlyTaskletStepConfiguration {
// job 생성
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job onlyTaskletJob() {
return this.jobBuilderFactory.get("onlyTaskletJob")
.start(onlyTaskletStep1())
.next(onlyTaskletStep2())
.build();
}
@Bean
public Step onlyTaskletStep1() {
return stepBuilderFactory.get("onlyTaskletStep1")
// TaskletStep - tasklet 저장
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("onlyTaskletStep1");
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Step onlyTaskletStep2() {
return stepBuilderFactory.get("onlyTaskletStep2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("실행되면 안되요");
return RepeatStatus.FINISHED;
}
})
.tasklet(new OnlyTasklet())
.build();
}
}
1) onlyTaskletStep2 에서 Tasklet을 2개 선언했다.
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("실행되면 안되요");
return RepeatStatus.FINISHED;
}
})
.tasklet(new OnlyTasklet()) // 여러개 불가능, 마지막 tasklet 실행
Tasklet 생성
OnlyTasklet.java
package com.project.springbatch._23_taskletStep._tasklet_여러개;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class OnlyTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
String stepName = stepContribution.getStepExecution().getStepName();
String jobName = chunkContext.getStepContext().getJobName();
System.out.println("jobName = " + jobName);
System.out.println("stepName = " + stepName);
return RepeatStatus.FINISHED;
}
}
배치 수행
결과
onlyTaskletStep1
jobName = onlyTaskletJob
stepName = onlyTaskletStep2
코드
@Bean
public Step onlyTaskletStep2() {
return stepBuilderFactory.get("onlyTaskletStep2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("실행되면 안되요");
return RepeatStatus.FINISHED;
}
})
.tasklet(new OnlyTasklet()) // 여러개 불가능, 마지막 tasklet 실행
.build();
}
첫번째 tasklet이 아닌 두번째로 선언된 OnlyTasklet 클래스가 수행됨을 확인할 수 있다.
반응형