Spring Batch Event Listeners
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:
- JobExecutionListener JavaDoc
- StepExecutionListener JavaDoc
- ItemReadListener JavaDoc
- ItemProcessListener JavaDoc
- ItemWriteListener JavaDoc
- StepSkipListener JavaDoc
Spring Batch Event Listeners的更多相关文章
- Spring Batch(8) -- Listeners
September 29, 2020 by Ayoosh Sharma In this article, we will take a deep dive into different types o ...
- Spring Batch的事务– Part 3: 略过和重试
原文:https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-3-skip-and-retry/ This i ...
- Spring Batch的事务-Part 1:基础
原文 https://blog.codecentric.de/en/2012/03/transactions-in-spring-batch-part-1-the-basics/ This is th ...
- Spring Batch Concepts Chapter
Spring Batch Concepts Chapter The below figure shows two kinds of Spring Batch components:infrastruc ...
- Spring Session event事件分析
1. org.apache.catalina.session.StandardSession 这是servlet-api jar包中的一个类.是session接口的标准实现.当session创建的时候 ...
- spring batch (四) Job的配置及配置文件说明介绍
内容来自<Spring Batch 批处理框架>,作者:刘相.我只是个搬运工. 一.Spring Batch提供了独立的标签用来顶一个Job配置,分别是job.step.tasklet.c ...
- Spring Batch批处理以及编程模型
1.批处理: 类似于SQL里面的批处理提交 2.场景: 业务定时进行批处理操作,但是批处理的编程模型是怎么的呢? 3.开源框架 Spring Batch 4.编程模型: reader-processo ...
- 批处理框架-spring Batch
并发处理业务 数据量大,并发度高,要支持事物,回滚,并发机制.事务.并发.监控.执行等,并不提供相应的调度功能.因此,如果我们希望批处理任务定期执行,可结合 Quartz 等成熟的调度框架实现. 业务 ...
- Spring batch学习 详细配置解读(3)
第一篇讲到普通job 配置 那么spring batch 给我们提供了丰富的配置,包括定时任务,校验,复合监听器,父类,重启机制等. 下面看一个动态设置读取文件的配置 1.动态文件读取 <?x ...
随机推荐
- 2021CISCN 华南赛区WEB wp
CISCN 华南区域赛 太菜了 我躺平了 easy_seri <?php error_reporting(0); highlight_file(__FILE__); class Test{ pu ...
- spring定时任务ThreadPoolTaskScheduler使用注意事项之线程池大小
背景 最近小伙伴解决了一个工单,描述为"手工推送案件无法推,提示token失效",当前工单状态为待关闭,解决方案为"东软接口不稳定造成的,东软的接口恢复正常后,问题解决& ...
- 菜鸡的Java笔记 - java 断言
断言:assert (了解) 所谓的断言指的是在程序编写的过程之中,确定代码执行到某行之后数据一定是某个期待的内容 范例:观察断言 public class Abnorma ...
- 【linux系统】命令学习(六)awk sed grep 与管道的使用
程序运行环境输入与输出 标准输入0 read a;echo $a 标准输出1 echo cesh 错误输出 ls notr 管道重定向 管道与管道之间可以重定向 管道与文件之间可以重定向 用于写入 将 ...
- [loj6254]最优卡组
特殊处理$c_{i}=1$的$i$,显然对这些$a_{i,1}$求和即可,以下都假设$c_{i}\ge 2$ 对于每一个$i$,将$a_{i,j}$从大到小排序:接下来,对于所有$i$,按照$a_{i ...
- [atARC105D]Let's Play Nim
先对$n$分奇偶两种情况考虑-- $n$为奇数,显然先手希望最终产生的$x_{1}\oplus x_{2}\oplus...\oplus x_{n}=0$ 对于后手,考虑构造:将最大的未被选择的$a_ ...
- Pickle的简单用法
Python中pickle的用法 pickle存在的意义 在python的文件操作里面,我们常常需要将python容器里面的一些东西把它写成一个二进制文件存放在硬盘里面来永久保存. 在不借助pickl ...
- 什么是H5
H5其实就是HTML5 常说的H5测试,就是移动端web页面,他跟安卓app与IOS app的区别: (1)以往的app是使用的原生系统内核的,相当于直接在系统上操作,是传统意义上的软件,更加稳定 ( ...
- java 单例模式实现代码
目录 1.使用静态内部类实现 2.使用枚举实现 3.序列化与反序列化 1.使用静态内部类实现 使用静态内部类实现单例模式,线程安全 class SingletonStaticInner { priva ...
- 学Web前端开发,选择培训学校是关键--青岛思途
互联网+的提出,催生了Web前端开发行业更大的就业空间,其行业热度也正呈爆炸式增长.专业人才供不应求导致了从业者薪资的居高不下,一般来说Web前端工程师的年薪可达15w以上,工作3~5年后通常可达到1 ...