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. lua入门之环境搭建、第一个demo

    前言 前段时间因为有些项目功能需要,自己研究了下lua,今天整理下,并以一个demo为示例演示 手机上的运行效果 分为几个步骤来逐步讲解. 1.lua介绍,为什么选择它? 2.环境安装 3.撸一个简单 ...

  2. asp.net中挺高性能的24种方法

    那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...

  3. CMU Convex Optimization(凸优化)笔记1--凸集和凸函数

    CMU凸优化笔记--凸集和凸函数 结束了一段时间的学习任务,于是打算做个总结.主要内容都是基于CMU的Ryan Tibshirani开设的Convex Optimization课程做的笔记.这里只摘了 ...

  4. python将字符串转换成对应的python数据类型--eval和json.loads(),json.dumps()

    eval()和json.loads() 都可以将字符串转换成对应的python数据类型,举个字典的例子,同样适合其他数据类型元组.列表.集合. In [3]: ss = '{"a" ...

  5. Ubuntu加速訪問GitHub

    Github一般用于Git的远程仓库,由于服务器位于国外,国内访问速度比较慢,为了提高访问速度,决定绕过DNS域名解析. 获取Github的IP地址 按下ctrl+alt+T打开命令终端,输入: ns ...

  6. 【Java】运行时Java对象在内存中是如何存储的?

    翻译自这一篇文章 我们知道函数在内存中实现为一个活动记录的栈.我们也知道Java方法在JVM栈区中实现为一个帧栈而Java对象是在堆区进行分配的. Java对象在堆内存中是怎样的呢?一旦对象保存在内存 ...

  7. [luogu7740]机器人游戏

    考虑容斥,令$f(S)$为要求$\forall p\in S,p$可以作为起点的方案数,答案即$\sum_{S\subseteq[0,n)}(-1)^{|S|}f(S)$ 关于计算$f(S)$,对于第 ...

  8. [atARC070F]HonestOrUnkind

    考虑当$a\le b$时,构造两种方案,满足诚实的人不交,接下来要求对于任意询问,这两种方案的答案都有可能相同 考虑询问$(i,j)$,若$i$在两种方案中有一种不诚实,那么总可以让答案相同,又因为诚 ...

  9. [atAGC106F]Figures

    考虑purfer序列,若生成树的pufer序列为$p_{i}$,则答案为$(\prod_{i=1}^{n}a_{i})\sum_{p}\prod_{i=1}^{n}\frac{(a_{i}-1)!}{ ...

  10. MySQL 在线开启&关闭GTID模式

    MySQL 在线开启&关闭GTID模式 目录 MySQL 在线开启&关闭GTID模式 基本概述 在线开启GTID 1. 设置GTID校验ENFORCE_GTID_CONSISTENCY ...