강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

최준성님의 프로필 이미지
최준성

작성한 질문수

스프링 배치

JobExecutionListener / StepExecutionListener

step endtime 질문

작성

·

504

0

안녕하세요. 

간단하게 질문이 있습니다.

StepExecutionListener의 afterStep 메서드에서 StepExecution의 endTime을 가져왔는데 null로 찍히는데 왜 null로 찍히는 건가요??

afterStep이니까 Step이 끝난 상태에서 동작하기 때문에 endTime이 찍힐 줄 알았는데 null이 찍혀서 궁금하여 질문드립니다.

퀴즈

스프링 배치에서 이벤트 리스너의 주된 역할은 무엇일까요?

배치 작업의 데이터 유효성 검사

작업, 스텝, 청크 실행 전후에 특정 로직 실행

데이터베이스 트랜잭션 관리

배치 작업에 사용될 데이터 소스 설정

답변 1

0

정수원님의 프로필 이미지
정수원
지식공유자

endTime 은 StepExecutionListener 이후에 값이 저장되고 있습니다.

finally {

try {
// Update the step execution to the latest known value so the
// listeners can act on it
exitStatus = exitStatus.and(stepExecution.getExitStatus());
stepExecution.setExitStatus(exitStatus);
exitStatus = exitStatus.and(getCompositeListener().afterStep(stepExecution));
}
catch (Exception e) {
logger.error(String.format("Exception in afterStep callback in step %s in job %s", name, stepExecution.getJobExecution().getJobInstance().getJobName()), e);
}

try {
getJobRepository().updateExecutionContext(stepExecution);
}
catch (Exception e) {
stepExecution.setStatus(BatchStatus.UNKNOWN);
exitStatus = exitStatus.and(ExitStatus.UNKNOWN);
stepExecution.addFailureException(e);
logger.error(String.format("Encountered an error saving batch meta data for step %s in job %s. "
+ "This job is now in an unknown state and should not be restarted.", name, stepExecution.getJobExecution().getJobInstance().getJobName()), e);
}

sample.stop(BatchMetrics.createTimer("step", "Step duration",
Tag.of("job.name", stepExecution.getJobExecution().getJobInstance().getJobName()),
Tag.of("name", stepExecution.getStepName()),
Tag.of("status", stepExecution.getExitStatus().getExitCode())
));
stepExecution.setEndTime(new Date());
stepExecution.setExitStatus(exitStatus);

step 이 실행하고 나서 최종적으로 값을 세팅하는 finally 구문인데 위 구문중

exitStatus = exitStatus.and(getCompositeListener().afterStep(stepExecution));

이 StepExecutionListener 의 afterStep 을 호출하는 구문이고

아래 쪽에 보시면

stepExecution.setEndTime(new Date());

으로 entTime 을 설정하고 있습니다.

그래서 StepExecutionListener 의 afterStep 시점에서는 endTime 이 null 상태가 맞습니다.

최준성님의 프로필 이미지
최준성

작성한 질문수

질문하기