15 Aralık 2022 Perşembe

SpringBatch StepListener Arayüzü

Giriş
Şu satırı dahil ederiz
import org.springframework.batch.core.StepListener;
Örnek
Elimizde şöyle bir kod olsun
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepListener;
import org.springframework.batch.core.annotation.AfterStep;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class StepCompletionNotificationListener implements StepListener {

  @BeforeStep
  public void beforeStep(StepExecution stepExecution){
    log.info("Step {} is started time {}",
      stepExecution.getStepName(),stepExecution.getStartTime());
    }

  @AfterStep
  public ExitStatus afterStep(StepExecution stepExecution){
    log.info("Step {} is ended time {}",
      stepExecution.getStepName(),stepExecution.getEndTime());
    return stepExecution.getExitStatus() == ExitStatus.COMPLETED ? 
      ExitStatus.COMPLETED : ExitStatus.FAILED;
  }

}
Şöyle yaparız
@Bean
public Step stepRealNewsReport(
  @Qualifier("readerNewsReportByRealStatus") ItemReader<NewsReport> itemReader, 
  @Qualifier("writerRealNewsReportTable") ItemWriter<RealNews> itemWriter, 
  StepCompletionNotificationListener listener) {

  return stepBuilderFactory
    .get("stepRealNewsReport")
    .listener(listener)
    .<NewsReport, RealNews>chunk(50)
    .reader(itemReader)
    .processor(realNewsProcessor())
    .writer(itemWriter)
    .build();
}

Hiç yorum yok:

Yorum Gönder