#11 spring batch: JobExecutionDecider
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package io.springbatch.basic.flow;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
|
||||
import org.springframework.batch.core.job.flow.JobExecutionDecider;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class CustomDecider implements JobExecutionDecider {
|
||||
|
||||
|
||||
@Override
|
||||
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
|
||||
|
||||
Random random = new Random();
|
||||
int count = random.nextInt(100);
|
||||
|
||||
return count % 2 == 0 ?
|
||||
new FlowExecutionStatus("EVEN") : new FlowExecutionStatus("ODD");
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
//@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class CustomExitStatusConfiguration {
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package io.springbatch.basic.flow;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.core.job.flow.JobExecutionDecider;
|
||||
import org.springframework.batch.core.launch.support.RunIdIncrementer;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JobExecutionDeciderConfiguration {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
/*
|
||||
JobExecutionDecider
|
||||
|
||||
- ExitStatus 를 조작하거나 StepExecutionListener 를 등록할 필요없이 Transition 처리를 위한 전용 클래스
|
||||
- Step 과 Transition 역할을 명확히 분리해서 설정 가능
|
||||
- Step 의 ExitStatus 가 아닌 JobExecutionDecider 의 FlowExecutionStatus 상태값을 새롭게 설정해서 반환
|
||||
|
||||
*/
|
||||
@Bean
|
||||
public Job batchJob() {
|
||||
return jobBuilderFactory.get("batchJob")
|
||||
.incrementer(new RunIdIncrementer())
|
||||
.start(step())
|
||||
.next(decider())
|
||||
.from(decider()).on("ODD").to(oddStep())
|
||||
.from(decider()).on("EVEN").to(evenStep())
|
||||
.end()
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JobExecutionDecider decider() {
|
||||
return new CustomDecider();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step() {
|
||||
return stepBuilderFactory.get("step")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println(">> This is the start tasklet");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step evenStep() {
|
||||
return stepBuilderFactory.get("evenStep")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println(">> EvenStep was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step oddStep() {
|
||||
return stepBuilderFactory.get("oddStep")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println(">> OddStep was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user