반응형
728x90
반응형
Job 생성
JobExecutionDeciderConfiguration.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.job.flow.JobExecutionDecider;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/*
--job.name=jobExecutionDeciderJob
*/
@Configuration
@RequiredArgsConstructor
public class JobExecutionDeciderConfiguration {
// job 생성
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job jobExecutionDeciderJob() {
return this.jobBuilderFactory.get("jobExecutionDeciderJob")
.incrementer(new RunIdIncrementer()) // job 여러번 실행 가능
.start(deciderEvenStartStep())
.next(decider())
/* SimpleFlow */
.from(decider()).on("ODD").to(deciderOddStep())
.from(decider()).on("EVEN").to(deciderEvenStep())
.end()
.build();
}
/**
* Decider Class
* @return
*/
@Bean
public JobExecutionDecider decider() {
return new CustomDecider();
}
@Bean
public Step deciderEvenStartStep() {
return stepBuilderFactory.get("deciderEvenStartStep")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("deciderEvenStartStep");
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Step deciderEvenStep() {
return stepBuilderFactory.get("deciderEvenStep")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("deciderEvenStep");
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Step deciderOddStep() {
return stepBuilderFactory.get("deciderOddStep")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("deciderOddStep");
return RepeatStatus.FINISHED;
}
})
.build();
}
}
1) Decider Class 설정
@Bean
public JobExecutionDecider decider() {
return new CustomDecider();
}
2) 실행 흐름 설정
@Bean
public Job jobExecutionDeciderJob() {
return this.jobBuilderFactory.get("jobExecutionDeciderJob")
.incrementer(new RunIdIncrementer()) // job 여러번 실행 가능
.start(deciderEvenStartStep())
.next(decider())
/* SimpleFlow */
.from(decider()).on("ODD").to(deciderOddStep())
.from(decider()).on("EVEN").to(deciderEvenStep())
.end()
.build();
}
Step | parttern | next Step |
deciderEvenStartStep() | ||
decider() | "ODD" | deciderOddStep() |
"EVEN" | deciderEvendStep() |
CustomDecider.java
package com.project.springbatch._32_decider;
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;
public class CustomDecider implements JobExecutionDecider {
private int count = 0;
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
count++;
if (count % 2 == 0) { // 짝
return new FlowExecutionStatus("EVEN");
} else { // 능
return new FlowExecutionStatus("ODD");
}
}
}
1) count 가 짝수일 경우 "EVEN", 홀수일 경우 "ODD"
if (count % 2 == 0) { // 짝
return new FlowExecutionStatus("EVEN");
} else { // 능
return new FlowExecutionStatus("ODD");
}
DB 테이블 조회
1) BATCH_JOB_EXECUTION
COLUMN | VALUE |
STATUS | COMPLETED |
EXIT_CODE | COMPLETED |
JOB_EXECUTION_ID | 23 |
2) BATCH_JOB_INSTANCE
COLUMN | VALUE |
JOB_NAME | jobExecutionDeciderJob |
3) BATCH_STEP_EXECUTION
- deciderEvenStartStep
COLUMN | VALUE |
STEP_NAME | deciderEvenStartStep |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
JOB_EXECUTION_ID | 23 |
- deciderOddStep
count: 1 이므로 deciderOddStep()이 수행되었다.
COLUMN | VALUE |
STEP_NAME | deciderOddStep |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
JOB_EXECUTION_ID | 23 |
반응형
'Coding > Spring Batch' 카테고리의 다른 글
[SpringBatch 실습] 22. ItemReader, ItemProcessor, ItemWriter의 메서드 직접 구현해보기 (0) | 2022.06.19 |
---|---|
[SpringBatch 실습] 21. Chunk 설정에 따른 reader(), processor(), writer() 수행 과정 (0) | 2022.06.18 |
[SpringBatch 실습] 19. EXIT_CODE를 직접 만들어서 적용해보기 (0) | 2022.06.13 |
[SpringBatch 실습] 18. Job, Flow 혼용하여 Job 수행시키기 (0) | 2022.06.12 |
[SpringBatch 실습] 17. JobFlowBuilder 흐름 제어하기 (Step 성공/실패에 따라 분기처리) (0) | 2022.06.10 |