반응형
목차
아래 포스팅에서 이어진 내용입니다.
job/stepExecutionListener를 이용해서 job, step 전후로 로그 출력하는 예제
jobTestConfig 🤗
package com.dev.lsy.springbatchremind.batch;
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.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@RequiredArgsConstructor
@Configuration
public class JobTestConfig {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
//job
public Job job1() {
return jobBuilderFactory.get("job1")
//jobParameter 자동 증감
.incrementer(new RunIdIncrementer())
.listener(new CustomJobExecutionListener())
.start(step1())
.next(step2())
.build();
}
@Bean
//step
public Step step1() {
return stepBuilderFactory.get("step1")
.tasklet(((contribution, chunkContext) -> {
log.info("=======================================");
log.info("여기는 step1입니다.");
log.info("=======================================");
return RepeatStatus.FINISHED;
}))
//step1에만 리스너 추가
.listener(new CustomStepExecutionListener())
.build();
}
@Bean
//step
public Step step2() {
return stepBuilderFactory.get("step2")
.tasklet(((contribution, chunkContext) -> {
log.info("=======================================");
log.info("여기는 step2입니다.");
log.info("=======================================");
return RepeatStatus.FINISHED;
}))
.build();
}
}
CustomJobExecutionListener 😊
package com.dev.lsy.springbatchremind.batch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
@Slf4j
public class CustomJobExecutionListener implements JobExecutionListener {
@Override
//job 시작 전 이벤트
public void beforeJob(JobExecution jobExecution) {
log.info("job이 시작되었다.");
log.info("jobName ==> [{}]", jobExecution.getJobInstance().getJobName());
}
@Override
//job 종료 후 이벤트
public void afterJob(JobExecution jobExecution) {
log.info("{}이 종료되었다.", jobExecution.getJobInstance().getJobName());
//jobExecution 이 친구를 이용해서 시작, 종료시간을 구할 수 있다.
long start = jobExecution.getStartTime().getTime();
long end = jobExecution.getEndTime().getTime();
log.info("job 소요시간 ==> [{}]", start - end);
}
}
CustomStepExecutionListener 🤗
package com.dev.lsy.springbatchremind.batch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
@Slf4j
public class CustomStepExecutionListener implements StepExecutionListener {
@Override
public void beforeStep(StepExecution stepExecution) {
log.info("스텝 시작 전~~~");
//데이터 추가
stepExecution.getExecutionContext().put("name", "ironMan");
}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
log.info("스텝 종료~~~");
//종료코드
log.info("exitStatus ==> [{}]", stepExecution.getExitStatus().getExitCode());
//beforeStep에서 설정한 데이터 출력
log.info("name ==> [{}]", stepExecution.getExecutionContext().get("name"));
return ExitStatus.COMPLETED;
}
}
개인 스터디 기록을 메모하는 공간이라 틀린점이 있을 수 있습니다.
틀린 점 있을 경우 댓글 부탁드립니다.
reference: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EB%B0%B0%EC%B9%98
다음 내용
반응형
'IT > development' 카테고리의 다른 글
[springBoot] spring batch AsyncProcess/Writer (61) | 2023.11.23 |
---|---|
[springBoot] spring batch multiThread process(feat. taskExecutor) (52) | 2023.11.22 |
[springBoot] spring batch 사용자 정의 ExitStatus (53) | 2023.11.18 |
[springBoot] spring batch flowJob (feat.simpleFlow) (55) | 2023.11.18 |
[springBoot] spring batch jsonFileItemWriter (55) | 2023.11.18 |