반응형
728x90
반응형
Job 생성
이전에 배웠었던 기본 Job
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.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
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=initJob
*/
@Configuration
@RequiredArgsConstructor
public class FlowJobConfiguration {
// job 생성
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job initJob() {
return this.jobBuilderFactory.get("initJob")
/* step start */
.start(initStep1())
.next(initStep2())
.build();
}
@Bean
public Step initStep1() {
return stepBuilderFactory.get("initStep1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("initStep1");
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Step initStep2() {
return stepBuilderFactory.get("initStep2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("initStep2");
return RepeatStatus.FINISHED;
}
})
.build();
}
}
Flow 객체를 사용하여 flowJob
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.builder.FlowBuilder;
import org.springframework.batch.core.job.flow.Flow;
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=flowJob
*/
@Configuration
@RequiredArgsConstructor
public class FlowJobConfiguration {
// job 생성
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job flowJob() {
return this.jobBuilderFactory.get("flowJob")
/* step start */
.start(flow())
.next(flowStep3())
.end()
.build();
}
/**
* FlowJob
* @return
*/
@Bean
public Flow flow() {
// "flow"
FlowBuilder<Flow> flowBuilder = new FlowBuilder<>("flow");
flowBuilder.start(flowStep1())
.next(flowStep2())
.end();
return flowBuilder.build();
}
@Bean
public Step flowStep1() {
return stepBuilderFactory.get("flowStep1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("flowStep1");
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Step flowStep2() {
return stepBuilderFactory.get("flowStep2")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("flowStep2");
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Step flowStep3() {
return stepBuilderFactory.get("flowStep3")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("flowStep3");
return RepeatStatus.FINISHED;
}
})
.build();
}
}
1) Flow 설정
@Bean
public Flow flow() {
FlowBuilder<Flow> flowBuilder = new FlowBuilder<>("flow");
flowBuilder.start(flowStep1())
.next(flowStep2())
.end();
return flowBuilder.build();
}
step 과 동일한 레벨로, 잡 안에 스텝이 있고 SimpleJob 안에서 스텝이 포함되어 실행되듯이, FlowJob 안에 flow 가 포함되어 실행된다.
2) flow 객체 전달
@Bean
public Job flowJob() {
return this.jobBuilderFactory.get("flowJob")
/* step start */
.start(flow())
.next(flowStep3())
.end()
.build();
}
jobBuilder의 start() 메서드가 FlowJobBuilder 를 생성한다. 그리고 내부적으로 start(flow)해서 JobFlowBuilder 를 생성한다.
해당 객체들이 생소하지만, 다음에 아키텍처를 배우면서 좀더 상세히 알아보자.
결과
flowStep1
flowStep2
flowStep3
DB 테이블 조회
1) BATCH_JOB_EXECUTION
COLUMN | VALUE |
STATUS | COMPLETED |
EXIT_CODE | COMPLETED |
JOB_EXECUTION_ID | 1 |
2) BATCH_JOB_INSTANCE
COLUMN | VALUE |
JOB_NAME | flowJob |
3) BATCH_STEP_EXECUTION
- flowStep1
COLUMN | VALUE |
STEP_NAME | flowStep1 |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
JOB_EXECUTION_ID | 1 |
- flowStep2
성공적으로 수행되었다.
COLUMN | VALUE |
STEP_NAME | flowStep2 |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
JOB_EXECUTION_ID | 1 |
- flowStep3
COLUMN | VALUE |
STEP_NAME | flowStep3 |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
JOB_EXECUTION_ID | 1 |
반응형