반응형
728x90
반응형
SpringBatch 시작하기
HelloJobConfiguration
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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=helloJob
*/
@RequiredArgsConstructor
@Slf4j
@Configuration // 빈 생성을 위해
public class HelloJobConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job helloJob() {
return this.jobBuilderFactory.get("helloJob")
/* step start */
.start(helloStep1())
.next(helloStep2())
.build();
}
@Bean
public Step helloStep1() {
return stepBuilderFactory.get("helloStep1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
log.info("hello step1");
return RepeatStatus.FINISHED;
}
})
.build();
}
public Step helloStep2() {
return stepBuilderFactory.get("helloStep2")
.tasklet((contribution, chunkContext) -> {
log.info("hello step2");
return RepeatStatus.FINISHED;
})
.build();
}
}
수행
application.yml
spring:
config:
activate:
on-profile: postgresql
datasource:
url: jdbc:postgresql://localhost:5432/...
username: ...
password: ...
driver-class-name: org.postgresql.Driver
batch:
job:
names: ${job.name:NONE} # --job.name=batchJob1 로 실행시키겠다는 의미. 아무런 파라미터로 넘기지 않으면 NONE이라는 이름으로 실행되어 아무런 배치잡이 수행되지 않는다.
IntelliJ Run/Debug Configurations
수행결과
hello step1
hello step2
DB 테이블 조회
1) BATCH_JOB_EXECUTION
COLUMN | VALUE |
STATUS | COMPLETED |
EXIT_CODE | COMPLETED |
2) BATCH_JOB_INSTANCE
COLUMN | VALUE |
JOB_NAME | HelloJob |
3) BATCH_STEP_EXECUTION
- helloStep1
COLUMN | VALUE |
STEP_NAME | helloStep1 |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
- helloStep2
COLUMN | VALUE |
STEP_NAME | helloStep2 |
EXIT_CODE | COMPLETED |
STATUS | COMPLETED |
해당 Job을 다시 수행한다면?
Step already complete or not restartable, so no action to execute: StepExecution: id=3, version=3, name=helloStep1, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
Step already complete or not restartable, so no action to execute: StepExecution: id=4, version=3, name=helloStep2, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
1) BATCH_JOB_EXECUTION
COLUMN | VALUE |
STATUS | COMPLETED |
EXIT_CODE | NOOP |
2) BATCH_JOB_INSTANCE
- 위 처음 실행했을때와 데이터가 새로 INSER 되지 않는다.
- 동일한 Job일때 jobInstance는 재사용한다. (동일한 Job : 동일한 파라미터로 COMPLETED Job 수행)
COLUMN | VALUE |
JOB_NAME | HelloJob |
3) BATCH_STEP_EXECUTION
- console에 위 오류 메시지가 출력되며, STEP이 실행되지 않는다.
포스팅 바로가기
준비중
반응형