[Quartz] Spring Batch Listener

S_H_H·2022년 10월 5일
0

Quartz And Batch

목록 보기
5/8

Spring Batch에서 사용했던 Listener를 적어볼려고 합니다.

  • JobExecutionListener
  • JobListenerSupport
  • StepExecutionListenerSupport

제가 사용한 Listener는 각 Job이 실행전, 후 Step이 실행전, 후를 기점으로 실행을 합니다.
해당 시점을 통해 어떤 Event처리가 필요하다면은 유용하게 사용하실 수 있어요

JobExecutionListener

public class TestJobListener implements JobExecutionListener {
    private final String jobName;

    public CustomJobListener(String jobName) {
        this.jobName = jobName;
    }

    @Override
    public void beforeJob(JobExecution jobExecution) {
        logger.info("beforeJob Name: {} Started.", jobName);
    }

    @Override
    public void afterJob(JobExecution jobExecution) {
        logger.info("afterJob Name: {} End.", jobName);
    }
}


    @Bean
    public Job testJob() {
        return jobBuilderFactory
        		.get("testJob")
                .incrementer(new RunIdIncrementer())
                .listener(new TestJobListener("testJob"))
                .start(testStep())
                .build();
    }

JobExecutionListener를 상속받은 클래스를 구현 후
jobBuilderFactory에서 상속 받은 클래스를 Listener에 넣어주시면 되겠습니다

  • beforeJob은 Job이 실행 전에 호출
  • afterJob은 Job이 실행 후에 호출

위 2시점에 Event를 넣을 수 있습니다.

JobListenerSupport

public class TestJobListenerSupport extends JobListenerSupport {

	@Override
	public String getName() {
		return this.getClass().getSimpleName();
	}

	@Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
		log.info("jobToBeExecuted Name: {} Started.", jobExecutionContext.getJobDetail().getKey());
    }

	@Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {
		log.info("jobExecutionVetoed Name: {} Started.", jobExecutionContext.getJobDetail().getKey());
    }

	@Override
	public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException jobException) {
		log.info("jobWasExecuted Name: {} Started.", jobExecutionContext.getJobDetail().getKey());
	}
    
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setGlobalJobListeners(TestJobListenerSupport());

JobListenerSupport를 상속받아 구현한 클래스를 구현 후
SchedulerFactoryBeanGlobalJobListeners로 등록하면 됩니다.

  • jobToBeExecuted Job이 호출되기 이전에 실행
  • jobExecutionVetoedvetoJobExecution 호출 시 실행 (중지 호출 시)
  • jobWasExecuted Job이 완료된 이후
    - JDBC를 사용한 다면 QRTZ_, Batch_ 의 테이블 Update 이후 시점

해당 Listener는 Job이 호출 전, 후로 실행되기 때문에
처음 안내한 Listener보다 이전에 실행되고, 이후에 실행하게 됩니다.

StepExecutionListenerSupport

public class TestStepListenerSupport extends StepExecutionListenerSupport {

	@Override
	public void beforeStep(StepExecution stepExecution) {
		log.info("beforeStep Name: {} Started.", stepExecution.getStepName());
	}

	@Override
	public ExitStatus afterStep(StepExecution stepExecution) {
		log.info("afterStep Name: {} Started.", stepExecution.getStepName());
	}
    
}    
    
	public Step testStep() {
		return stepBuilderFactory
				.get("testSetp")
				.<Test, Test>chunk(1000)
				.reader(testReader())
				.writer(testWriter())
				.listener(new TestStepListenerSupport())
				.build();
	}    

StepExecutionListenerSupport 상속받아 클래스 구현 후
stepBuilderFactorylistener로 등록해주시면 됩니다.

  • beforeStep Step이 실행되기 전
  • afterStep Step이 실행 된 후

만약 한 Job에 여러 Step이 있을 경우 해당 Step 실행 전, 후 마다 Listenter가 호출 되게 됩니다.

순서 정리

제가 설명드린 위 3의 Listener를 모두 사용한다면 Log에 찍히는 순서는

jobToBeExecuted Name: testJob Started.
beforeJob Name: testJob Started.
beforeStep Name: testStep Started.
afterStep Name: testStep Started.
afterJob Name: testJob End.
jobWasExecuted Name: testJob Started.

JobListenerSupport (jobToBeExecuted) -> JobExecutionListener (beforeJob) -> StepExecutionListenerSupport (beforeStep) -> StepExecutionListenerSupport (afterStep) -> JobExecutionListener (afterJob) -> JobListenerSupport (jobWasExecuted)

위와 같은 순서로 처리되게 됩니다.

그 외

  • ChunkListener
  • ItemReadListener
  • ItemProcessListener
  • ItemWriteListener
  • SkipListener

Spring Batch Docs에서 확인해보시면 될 것 같아요

profile
LEVEL UP

0개의 댓글