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.

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的更多相关文章

  1. Spring Batch(8) -- Listeners

    September 29, 2020 by Ayoosh Sharma In this article, we will take a deep dive into different types o ...

  2. Spring Batch的事务– Part 3: 略过和重试

    原文:https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-3-skip-and-retry/ This i ...

  3. Spring Batch的事务-Part 1:基础

    原文 https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/ This is th ...

  4. Spring Batch Concepts Chapter

    Spring Batch Concepts Chapter The below figure shows two kinds of Spring Batch components:infrastruc ...

  5. Spring Session event事件分析

    1. org.apache.catalina.session.StandardSession 这是servlet-api jar包中的一个类.是session接口的标准实现.当session创建的时候 ...

  6. spring batch (四) Job的配置及配置文件说明介绍

    内容来自<Spring Batch 批处理框架>,作者:刘相.我只是个搬运工. 一.Spring Batch提供了独立的标签用来顶一个Job配置,分别是job.step.tasklet.c ...

  7. Spring Batch批处理以及编程模型

    1.批处理: 类似于SQL里面的批处理提交 2.场景: 业务定时进行批处理操作,但是批处理的编程模型是怎么的呢? 3.开源框架 Spring Batch 4.编程模型: reader-processo ...

  8. 批处理框架-spring Batch

    并发处理业务 数据量大,并发度高,要支持事物,回滚,并发机制.事务.并发.监控.执行等,并不提供相应的调度功能.因此,如果我们希望批处理任务定期执行,可结合 Quartz 等成熟的调度框架实现. 业务 ...

  9. Spring batch学习 详细配置解读(3)

    第一篇讲到普通job 配置 那么spring  batch 给我们提供了丰富的配置,包括定时任务,校验,复合监听器,父类,重启机制等. 下面看一个动态设置读取文件的配置 1.动态文件读取 <?x ...

随机推荐

  1. spring boot+vue实现H5聊天室客服功能

    spring boot+vue实现H5聊天室客服功能 h5效果图 vue效果图 功能实现 spring boot + webSocket 实现 官方地址 https://docs.spring.io/ ...

  2. C 语言基础,来喽!

    前言 C 语言是一门抽象的.面向过程的语言,C 语言广泛应用于底层开发,C 语言在计算机体系中占据着不可替代的作用,可以说 C 语言是编程的基础,也就是说,不管你学习任何语言,都应该把 C 语言放在首 ...

  3. Python科普系列——类与方法(上篇)

    欢迎来到新的系列,up又开新坑了~~ 实际上,Python作为一门易用性见长的语言,看上去简单,却仍然有很多值得一说的内容,因此这个系列会把Python中比较有意思的地方也给科普一遍.而另一方面,关于 ...

  4. 团队内部密码共享方案:KeePassXC+微盘(企业微信)

    目录 需求描述 适用场景 安装使用 KeePassXC初始化 浏览器插件安装设置 1.火狐 2.Edge 3.Chrome 软件-插件的链接 登陆网站并保存密码 (企业微信)微盘共享内部数据库 其他 ...

  5. 15-Transfer Learning

    介绍 迁移学习指的就是,假设你手上有一些跟你现在要进行的task没有直接相关的data,那你能不能用这些没有直接相关的data来帮助我们做一些什么事情.比如说:你现在做的是猫跟狗的classifer, ...

  6. 关于JAVA中顺序IO的基本操作

    关于JAVA中顺序IO的基本操作 写在前面 最近研究一下JAVA中的顺序IO,在网络上找了一会儿,发现少有详细的介绍,顾此在此处说说顺序IO,才学疏浅,如有不对,望赐教. 什么是顺序IO 事实上JAV ...

  7. [cf1491H]Yuezheng Ling and Dynamic Tree

    将其按照区间分块(即$[(i-1)K+1,iK]$作为一个块),并定义$f_{x}$表示$x$的祖先中编号最小且与$x$在同一个块内的节点,$f_{x}$可以通过$f_{a_{x}}$转移,即$f_{ ...

  8. [luogu5423]Valleys

    先考虑不要求有洞,那么可以将所有权值排序,然后不断插入,那么一个连通块就是一个答案,加上连通块大小即可考虑并查集如何判断是否有洞,可以发现对于任意一个无洞的直角多边形,都有$90度内角-90度外角=4 ...

  9. Python系列教程-详细版 | 图文+代码,快速搞定Python编程(附全套速查表)

    作者:韩信子@ShowMeAI 教程地址:http://showmeai.tech/article-detail/python-tutorial 声明:版权所有,转载请联系平台与作者并注明出处 引言 ...

  10. 雇工模式(Employee Pattern)

    本文节选自<设计模式就该这样学> 1 雇工模式的定义 雇工模式(Employee Pattern)也叫作仆人模式(Servant Pattern),属于行为型设计模式,它为一组类提供通用的 ...