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. lombok标签之@Data @AllArgsConstructor @@NoArgsConstructor -如何去除get,set方法。@Data注解和如何使用,lombok

    在代码中我们可以只加上标签@Data 而不用get,set方法: val : 和 scala 中 val 同名, 可以在运行时确定类型; @NonNull : 注解在参数上, 如果该类参数为 null ...

  2. MySQL基础语句(修改)

    ①INSERT INSERT INTO students (class_id, name, gender, score) VALUES (2, '大牛', 'M', 80); 向students表插入 ...

  3. GitHub出现Permission denied (publickey)

    Permission denied (publickey) 没有权限的publickey 重新生成一次ssh key即可解决 ssh-keygen -t rsa -C "这里输入你的邮箱&q ...

  4. hover 背后的数学和图形学

    前端开发中,hover是最常见的鼠标操作行为之一,用起来也很方便,CSS直接提供:hover伪类,js可以通过mouseover+mouseout事件模拟,甚至一些第三方库/框架直接提供了 hover ...

  5. Spring Cloud Gateway实战之五:内置filter

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. vue+node+mongondb实战之mongodb登陆操作

    页面搭建基本完成,只是样式还没有美化,由于采取的前后端分离开发,所有页面逻辑全部由vue来负责,后台采用express框架只用来提供 接口,注册就是讲数据存入数据库,比较简单,而登陆碰了一些小问题,发 ...

  7. NodeJS连接MongoDB和mongoose

    1.MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案.是世界上目前用的最广泛的nosql数据库 2.noSql 翻译过来 not o ...

  8. [hdu6316]Odd shops

    记$m=10$,即商品的种类 记$g(x)=1+\sum_{i=1}^{m}a_{i}x_{i}$,问题即求$f_{n}(x)=g^{n}(x)$非0项数(模2意义下) 注意到$f^{2}(x)\eq ...

  9. [bzoj5294]二进制

    首先可以发现$2^k$模3意义下有循环节,也就是1,-1,1,-1--考虑对于x个1,y个0,判断是否存在3的倍数1.x为偶数时一定可以,选择等量的1和-1即可2.x为奇数,要满足$x\ge 3$且$ ...

  10. javaweb监听

    监听项目启动 package com.java7115.quartz; import javax.servlet.ServletContextEvent; import javax.servlet.S ...