Coding/Spring Batch
[스프링 배치] 스텝 중지시키기 setTerminateOnly()
shbada
2021. 9. 25. 11:26
728x90
반응형
StepExecution 스텝 중지
beforeStep 에서 StepExecution 을 가져온 다음, 이후 실행될 스텝에서 setTerminateOnly() 메서드를 사용한다. setTerminateOnly() 메서드를 호출하면, 스텝이 완료된 후 스프링 배치가 종료된다. 아래 예제처럼 if문의 조건에 충족하지 못할시 해당 스텝 완료 후 스프링 배치를 종료시킬 수 있다.
public class TransactionReader implements ItemStreamReader<Transaction> {
...
/**
* afterStep 이 아닌 beforeStep 을 사용하도록 변경하여 StepExecution 을 가져온다.
*/
private StepExecution stepExecution;
...
/* 스텝 빌드 전 stepExecution set */
@BeforeStep
public void beforeStep(StepExecution execution) {
this.stepExecution = execution;
}
...
private Transaction process(FieldSet fieldSet) {
...
/* stepExecution : setTerminateOnly (스텝이 완료된 후 스프링 배치가 종료되도록 지시하는 플래그 설정 ) */
if(expectedRecordCount != this.recordCount) {
this.stepExecution.setTerminateOnly();
}
return result;
}
}
StepExecution 을 이용해 잡이 중지되었다. 위 예제는 ExitStatus.STOPPED 상태로 스텝이 완료된 후 잡이 중지되었다.
반응형