首页 > 编程语言 > 详细

Spring Batch Event Listeners

时间:2021-03-31 14:06:19      阅读:42      评论:0      收藏:0      [点我收藏+]

Learn to create and configure Spring batch’s JobExecutionListener (before and after job), StepExecutionListener (before and after step), ItemReadListener, ItemProcessListener, ItemWriteListener and SkipListener implementations with example.

  • TOC
    {: toc}

JobExecutionListener

JobExecutionListener Listener Example

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
 
public class JobResultListener implements JobExecutionListener {
 
    public void beforeJob(JobExecution jobExecution) {
        System.out.println("Called beforeJob().");
    }
 
    public void afterJob(JobExecution jobExecution) {
        System.out.println("Called afterJob().");
    }
}

How to configure JobExecutionListener

@Bean
public Job demoJob(){
    return jobs.get("demoJob")
            .incrementer(new RunIdIncrementer())
            .listener(new JobResultListener())
            .start(stepOne())
            .next(stepTwo())
            .build()

StepExecutionListener

StepExecutionListener Listener Example

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
 
public class StepResultListener implements StepExecutionListener {
 
    @Override
    public void beforeStep(StepExecution stepExecution) {
        System.out.println("Called beforeStep().");
    }
 
    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        System.out.println("Called afterStep().");
        return ExitStatus.COMPLETED;
    }
}

How to configure StepExecutionListener

@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepResultListener())
            .build();
}
  
@Bean
public Step stepTwo(){
    return steps.get("stepTwo")
            .tasklet(new MyTaskTwo())
            .listener(new StepResultListener())
            .build();
}  

ItemReadListener

ItemReadListener Listener Example

import org.springframework.batch.core.ItemReadListener;
 
public class StepItemReadListener implements ItemReadListener<String> {
 
    @Override
    public void beforeRead() {
        System.out.println("ItemReadListener - beforeRead");
    }
 
    @Override
    public void afterRead(String item) {
        System.out.println("ItemReadListener - afterRead");
    }
 
    @Override
    public void onReadError(Exception ex) {
        System.out.println("ItemReadListener - onReadError");
    }
}

How to configure ItemReadListener

@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepItemReadListener())
            .build();
}

ItemProcessListener

ItemProcessListener Listener Example

import org.springframework.batch.core.ItemProcessListener;
 
public class StepItemProcessListener implements ItemProcessListener<String, Number> {
 
    @Override
    public void beforeProcess(String item) {
        System.out.println("ItemProcessListener - beforeProcess");
    }
 
    @Override
    public void afterProcess(String item, Number result) {
        System.out.println("ItemProcessListener - afterProcess");
    }
 
    @Override
    public void onProcessError(String item, Exception e) {
        System.out.println("ItemProcessListener - onProcessError");
    }
}

How to configure ItemProcessListener

@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepItemProcessListener())
            .build();
}

ItemWriteListener

ItemWriteListener Listener Example

import java.util.List;
import org.springframework.batch.core.ItemWriteListener;
 
public class StepItemWriteListener implements ItemWriteListener<Number> {
 
    @Override
    public void beforeWrite(List<? extends Number> items) {
        System.out.println("ItemWriteListener - beforeWrite");
    }
 
    @Override
    public void afterWrite(List<? extends Number> items) {
        System.out.println("ItemWriteListener - afterWrite");
    }
 
    @Override
    public void onWriteError(Exception exception, List<? extends Number> items) {
        System.out.println("ItemWriteListener - onWriteError");
    }
}

How to configure ItemWriteListener

@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepItemWriteListener())
            .build();
}

SkipListener

SkipListener Listener Example

import org.springframework.batch.core.SkipListener;
 
public class StepSkipListener implements SkipListener<String, Number> {
 
    @Override
    public void onSkipInRead(Throwable t) {
        System.out.println("StepSkipListener - onSkipInRead");
    }
 
    @Override
    public void onSkipInWrite(Number item, Throwable t) {
        System.out.println("StepSkipListener - afterWrite");
    }
 
    @Override
    public void onSkipInProcess(String item, Throwable t) {
        System.out.println("StepSkipListener - onWriteError");
    }
}

How to configure SkipListener

@Bean
public Step stepOne(){
    return steps.get("stepOne")
            .tasklet(new MyTaskOne())
            .listener(new StepSkipListener())
            .build();
}

It’s very simple to use and implement. Let me know your questions in comments section.

Happy Learning !!

References:

  1. JobExecutionListener JavaDoc
  2. StepExecutionListener JavaDoc
  3. ItemReadListener JavaDoc
  4. ItemProcessListener JavaDoc
  5. ItemWriteListener JavaDoc
  6. [StepSkipListener JavaDoc](

Spring Batch Event Listeners

原文:https://www.cnblogs.com/satire/p/14600944.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!