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 ...
随机推荐
- Unicode、UTF-8、UTF-16 终于懂了
计算机起源于美国,上个世纪,他们对英语字符与二进制位之间的关系做了统一规定,并制定了一套字符编码规则,这套编码规则被称为ASCII编码 ASCII 编码一共定义了128个字符的编码规则,用七位二进制表 ...
- ARM汇编解决阶乘以及大小写转换
环境以及硬件 一.硬件仿真基于 SAMSUNG's S3C44B0X 16/32-bit RISC microprocessor 芯片,仿真器为 J-LINK 二.编写指令软件为 Integrated ...
- GitHub出现Permission denied (publickey)
Permission denied (publickey) 没有权限的publickey 重新生成一次ssh key即可解决 ssh-keygen -t rsa -C "这里输入你的邮箱&q ...
- mysql 数据库中 int(3) 和 int(11) 有区别么???
今天去面试的时候 面试官问到了这个问题:int(3) 和 int(11) 有什么区别?? 当时一听有点蒙,(不知道为什么蒙,后来回来想想可能是觉得考官怎么会问这么简单的问题呢,所以蒙了),当时我的回答 ...
- Excel - java
package com.e6soft.project.ExcelUtil; import java.io.BufferedInputStream; import java.io.File; impor ...
- PAT A1060——string的常见用法详解
string 常用函数实例 (1)operator += 可以将两个string直接拼接起来 (2)compare operator 可以直接使用==.!=.<.<=.>.>= ...
- bean注解
1.beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi=&qu ...
- 【Java面试】-- 杂题
杂题 2019-11-03 21:09:37 by冲冲 1.类加载器的双亲委派机制 类加载器:把类通过类加载器加载到JVM中,然后转换成class对象(通过类的全路径来找到这个类). 双亲委派机制 ...
- PCA方差解释比例求解与绘图?
目录 主成分方差解释率计算 绘图示例 主成分方差解释率计算 通常,求得了PCA降维后的特征值,我们就可以绘图,但各个维度的方差解释率没有得到,就无法获得PC坐标的百分比. 有些工具的结果是提供了维度标 ...
- dokuwiki使用随笔
1. 在dokuwiki上安装MataJax插件后: a. $....$:之间书写数学公式;如完全平方公式:$a^2+b^2+2ab$,该公式将与当前行文字处于同一行; b. $$ .... $$ 之 ...