반응형
728x90
반응형
Tasklet 클래스 생성
CustomTasklet.java
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 CustomTasklet implements Tasklet {
/**
* 비즈니스 로직 구현
* @param stepContribution
* @param chunkContext
* @return
* @throws Exception
*/
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("taskletTestStep2");
return RepeatStatus.FINISHED;
}
}
CustomTaskletStepConfiguration.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=stepTaskletTestJob
*/
@Configuration
@RequiredArgsConstructor
public class CustomTaskletStepConfiguration {
// job 생성
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job stepTaskletTestJob() {
return this.jobBuilderFactory.get("stepTaskletTestJob")
/* 각각의 스텝은 독립적으로 생성되어 Tasklet 이 생성된다.
* Step 은 자기만의 Tasklet 을 가진다. */
.start(taskletTestStep1())
.next(taskletTestStep2())
.build();
}
@Bean
public Step taskletTestStep1() {
return stepBuilderFactory.get("taskletTestStep1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
System.out.println("taskletTestStep1");
return RepeatStatus.FINISHED;
}
})
.build();
}
@Bean
public Step taskletTestStep2() {
return stepBuilderFactory.get("taskletTestStep2")
// 빈으로 등록해도되고, 이렇게 객체 생성해도 된다.
.tasklet(new CustomTasklet())
.build();
}
}
- taskletTestStep2에서 CustomTasklet 클래스를 설정한다.
@Bean
public Step taskletTestStep2() {
return stepBuilderFactory.get("taskletTestStep2")
// 빈으로 등록해도되고, 이렇게 객체 생성해도 된다.
.tasklet(new CustomTasklet())
.build();
}
- 결과
taskletTestStep1
taskletTestStep2
DB 테이블 조회
1) BATCH_JOB_EXECUTION
COLUMN | VALUE |
STATUS | COMPLETED |
EXIT_CODE | COMPLETED |
2) BATCH_JOB_INSTANCE
COLUMN | VALUE |
JOB_NAME | stepTaskletTestJob |
3) BATCH_STEP_EXECUTION
- taskletTestStep1
COLUMN | VALUE |
STEP_NAME | taskletTestStep1 |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
- taskletTestStep2
COLUMN | VALUE |
STEP_NAME | taskletTestStep2 |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
반응형
'Coding > Spring Batch' 카테고리의 다른 글
[SpringBatch 실습] 6. ExecutionContext (StepExecution, JobExecution 관계) 예제로 흐름 파악하기 (0) | 2022.05.24 |
---|---|
[SpringBatch 실습] 5. Batch Job 수행시 Step 실패하는 경우2 (Step 실패와 StepExceution 관계) (0) | 2022.05.23 |
[SpringBatch 실습] 3. Batch Job 수행시 Step 실패하는 경우 (0) | 2022.05.22 |
[SpringBatch 실습] 2. ApplicationRunner 구현 클래스로 Job 수행시키기 (0) | 2022.05.21 |
[SringBatch 실습] 1. Hello Spring Batch! (0) | 2022.05.21 |