这里我们对上篇博客的例子做一个修改性的测试来学习一下springbatch的一些关于chunk的一些有用的特性。我渐渐能意会到,深刻并不等于接近事实。

springbatch的学习

一、chunk的skip-limit属性的使用

  关于这个属性的介绍:Maximum number of skips during processing of the step. If processing reaches the skip limit, the next exception thrown on item processing (read, process, or write) causes the step to fail.

我们修改batch.xml里面的关于readWriter里面的设置属性。如下:

<!-- old -->
<step id="readWriter" next="clean">
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="100" processor="processor">
</chunk>
</tasklet>
</step> <!-- new -->
<step id="readWriter" next="clean">
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="100" skip-limit="2" processor="processor">
<skippable-exception-classes>
<include class="org.springframework.batch.item.file.FlatFileParseException"/>
</skippable-exception-classes>
</chunk>
</tasklet>
</step>

  FlatFileParseException:Exception thrown when errors are encountered parsing flat files.修改的解压文件的内容,让它有一条数据是错误的。如下:这个日期肯定是错误的,当然这里是为了测试skip-limit属性才做如此的方法处理。其实面对这样的数据,可以放在process里面进行过滤处理的。

运行后的结果,数据库的数据如下:

可以看到上述的那条错误数据没有插入到表中,但是正常的数据已经插入到数据库中。如果增加解压文件的错误条数。比如3条的时候。控制台会报错:org.springframework.batch.core.step.skip.SkipLimitExceededException: Skip limit of '2' exceeded。数据库表的数据也没有成功的插入。

二、chunk的skip-policy属性的使用

  如果在意异常数量的话,用上述的skip-limit比较方便和简单。如果不在意异常数量的话,我们可以自己定义忽略的策略,也就是这段要学习的部分。修改batch.xml里面的关于readWriter里面的设置属性如下:

<step id="readWriter" next="clean">
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="100" skip-policy="skipPolicy" processor="processor"/>
</tasklet>
</step>
<step id="clean">
<tasklet ref="cleanTasklet"/>
</step>

在job.xml中声明定义skipPolicy,内容如下

<bean id="skipPolicy" class="spring.batch.readFile.ExceptionSkipPolicy">
<constructor-arg value="org.springframework.batch.item.file.FlatFileParseException"/>
</bean>

ExceptionSkipPolicy是我们自定义的异常策略实现类

package spring.batch.readFile;

import org.springframework.batch.core.step.skip.SkipLimitExceededException;
import org.springframework.batch.core.step.skip.SkipPolicy; /**
* @Author: huhx
* @Date: 2017-11-01 下午 4:58
*/
public class ExceptionSkipPolicy implements SkipPolicy { private Class<? extends Exception> exceptionClassToSkip; public ExceptionSkipPolicy(Class<? extends Exception> exceptionClassToSkip) {
super();
this.exceptionClassToSkip = exceptionClassToSkip;
} @Override
public boolean shouldSkip(Throwable t, int skipCount) throws SkipLimitExceededException {
return exceptionClassToSkip.isAssignableFrom(t.getClass());
}
}

修改解压文件的内容,其实就是上述的错误3条数据的内容。如下

运行之后的数据库数据如下:

三、SkipListener监听skip的数据

我们基于上述做的修改,现在的job节点xml配置如下:

<job id="readFlatFileJob">
<step id="decompress" next="readWriter">
<tasklet ref="decompressTasklet"/>
</step>
<step id="readWriter" next="clean">
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="100" skip-policy="skipPolicy" processor="processor"/>
<listeners>
<listener ref="skipListener"/>
</listeners>
</tasklet>
</step>
<step id="clean">
<tasklet ref="cleanTasklet"/>
</step>
</job>

job.xml中配置skipListener

<bean id="skipListener" class="spring.batch.readFile.FileSkipListener"/>

FileSkipListener的代码如下:

package spring.batch.readFile;

import org.apache.commons.io.FileUtils;
import org.springframework.batch.core.annotation.OnSkipInProcess;
import org.springframework.batch.core.annotation.OnSkipInRead;
import org.springframework.batch.core.annotation.OnSkipInWrite;
import org.springframework.batch.item.file.FlatFileParseException; import java.io.File;
import java.io.IOException; /**
* @Author: huhx
* @Date: 2017-11-01 下午 5:32
*/
public class FileSkipListener {
private File file = new File("file/log.txt"); @OnSkipInRead
public void readLog(Throwable t) throws IOException {
if (t instanceof FlatFileParseException) {
FlatFileParseException ffpe = (FlatFileParseException) t;
String dataLog = "from read " + ffpe.getInput() + ", line number = " + ffpe.getLineNumber() + "\n";
FileUtils.write(file, dataLog, true);
}
} @OnSkipInProcess
public void processLog(People people, Throwable t) throws IOException {
if (t instanceof FlatFileParseException) {
FlatFileParseException ffpe = (FlatFileParseException) t;
String dataLog = "from process " + ffpe.getInput() + ", line number = " + ffpe.getLineNumber() + "\n";
String peopleInfo = people.getUsername() + ", address " + people.getBirthday() + "\n";
FileUtils.write(file, dataLog + peopleInfo, true);
}
} @OnSkipInWrite
public void writeLog(People people, Throwable t) throws IOException {
if (t instanceof FlatFileParseException) {
FlatFileParseException ffpe = (FlatFileParseException) t;
String dataLog = "from write " + ffpe.getInput() + ", line number = " + ffpe.getLineNumber() + "\n";
String peopleInfo = people.getUsername() + ", address " + people.getBirthday() + "\n";
FileUtils.write(file, dataLog + peopleInfo, true);
}
}
}

对于上述错误的几条记录,我们记日志在log.txt里面。现在log.txt的内容如下:

from read 李元芳||黄冈|--, line number =
from read 王昭君|百里|武汉|--, line number =
from read 狄仁杰||天津|--, line number =

springbatch提供的SkipListener接口去监听skip的数据项。

public interface SkipListener<T,S> extends StepListener {
void onSkipInRead(Throwable t);
void onSkipInProcess(T item, Throwable t);
void onSkipInWrite(S item, Throwable t);
}

当然比较方便的一种做法,就是springbatch提供的上述的@OnSkipInRead,@OnSkipInProcess and @OnSkipInWrite注解方式。

四、类似于上述的skip策略,springbatch还支持retry(重试)的功能

定义重试的方式有二种,和skip的类似。这里我们列举如下:

  • 默认retry策略的可以定义重试次数的方式:
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="100" retry-limit="3">
<retryable-exception-classes>
<include class="org.springframework.daoOptimisticLockingFailureException" />
</retryable-exception-classes>
</chunk>
</tasklet>
  • 自定义重试策略的方式:
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="100" retry-policy="retryPolicy" />
</tasklet>

retryPolicy的定义如下:

<bean id="retryPolicy" class="org.springframework.retry.policy.ExceptionClassifierRetryPolicy">
<property name="policyMap">
<map>
<entry key="org.springframework.dao.ConcurrencyFailureException">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="3"/>
</bean>
</entry>
<entry key="org.springframework.dao.DeadlockLoserDataAccessException">
<bean class="org.springframework.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="5"/>
</bean>
</entry>
</map>
</property>
</bean>

当然,retry也有类似于skip的SkipListener。操作及用法如下

package spring.batch.readFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.listener.RetryListenerSupport; /**
* @Author: huhx
* @Date: 2017-11-01 下午 7:00
*/
public class Slf4jRetryListener extends RetryListenerSupport {
private static final Logger LOG = LoggerFactory.getLogger(Slf4jRetryListener.class); @Override
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
LOG.error("retried operation",throwable);
}
}

友情链接

springbatch---->springbatch的使用(三)的更多相关文章

  1. SpringBatch Sample (三)(XML文件操作)

    前篇关于Spring Batch的文章,讲述了Spring Batch 对CSV文件的读写操作. 本文将通过一个完整的实例,与大家一起讨论运用Spring Batch对XML文件的读写操作.实例流程是 ...

  2. YII内置验证规则

    required: 必填字段验证, 来自 CRequiredValidator类的别名 array(‘字段名列表用逗号隔开’, ‘required’),    就这样的一个小小的写法,可以让字段前面加 ...

  3. Spring Batch介绍

    简介 SpringBatch 是一个大数据量的并行处理框架.通常用于数据的离线迁移,和数据处理,⽀持事务.并发.流程.监控.纵向和横向扩展,提供统⼀的接⼝管理和任务管理;SpringBatch是Spr ...

  4. SpringBatch的初步了解

    一.SpringBatch是一个批处理的框架,作为一个Spring组件,提供了通过使用Spring的依赖注入来处理批处理的条件. 什么是批处理呢? 在现代企业应用当中,面对复杂的业务以及海量的数据,除 ...

  5. Spring-boot+Spring-batch+hibernate+Quartz简单批量读文件写数据用例

    本文程序集成了Spring-boot.Spring-batch.Spring-data-jpa.hibernate.Quartz.H2等.完整代码在Github上共享,地址https://github ...

  6. springBatch学习 batch的使用方式(5)

    首先讲一下batch框架提供的一组job执行的api 如下图 说明: 应用场景 包含三种 标准的web ,定时任务调度,命令行 1.命令行 通过命令行在单独的jvm中调用 进行批处理作业 spring ...

  7. SpringBoot整合SpringBatch实用简例

    SpringBatch主要是一个轻量级的大数据量的并行处理(批处理)的框架. 作用和Hadoop很相似,不过Hadoop是基于重量级的分布式环境(处理巨量数据),而SpringBatch是基于轻量的应 ...

  8. SpringBatch异常To use the default BatchConfigurer the context must contain no more thanone DataSource

    SpringBoot整合SpringBatch项目,已将代码开源至github,访问地址:https://github.com/cmlbeliever/SpringBatch 欢迎star or fo ...

  9. SpringBoot整合SpringBatch

    一.引入依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...

  10. 业务可视化-让你的流程图"Run"起来(6.定时任务&Spring-Batch的集成)

    前言 首先,感谢大家对上一篇文章[业务可视化-让你的流程图"Run"起来(5.SpringBoot集成&微服务编排)]的支持. 分享一下近期我对这个项目的一些改进. 在项目 ...

随机推荐

  1. MySQL中mysqldump导出数据的使用

    mysqldump常用参数说明: 帮助使用:mysqldump --help -A, --all-databases 导出全部数据库 mysqldump -uroot -p –A > /tmp/ ...

  2. 特殊权限set_uid /特殊权限set_gid/特殊权限stick_bit/软链接文件/硬连接文件

    2.18 特殊权限set_uid 2.19 特殊权限set_gid 2.20 特殊权限stick_bit 2.21 软链接文件 2.22 硬连接文件 特殊权限set_uid(s权限用户user权限) ...

  3. 【python】命令行输出颜色

    http://www.cnblogs.com/chjbbs/p/5706513.html

  4. 续:纠正:debian【4】可以安装,而且完美的安装 ! for《Oracle-10.2.0.1,打补丁10.2.0.5:在 debian 版本4【不含4】以上,及 ubuntu 7.04【不含7.04】以上都可以安装!》

    关键点: ip a ifconfig -a dhclient ifconfig -a poweroff ip a ifconfig -a apt-get update apt-get install ...

  5. Can't clobber writable file **************

    最近搭建了新的quick check server, workspace也是新的.但是get latest (unshelve)的时候,出现以下错误: can't clobber writable f ...

  6. 怎么设置BarTender中二维码大小为25*25

    有小伙伴近期问了小编一个问题,说客户需要25*25大小的QR Code二维码,用BarTender怎么做出来?想要指定条形码的大小,还得BarTender符号与版本选项来帮忙.本文小编就来给大家详细讲 ...

  7. 栈空间默认1M,测试存进数据时间

    #include <stdio.h> 栈空间是1024*1024,一兆1M,其中包含了进入main函数之前的1万左右空间.全空间是足的.速度:栈>全局>堆 测试运算时间.100 ...

  8. 5种实现垂直居中css

    摘要: 在我们制作页面的时候经常会遇到内容垂直居中的需求,今天分享5种垂直居中的方法,每种方法都有自己的优缺点,可以选择自己喜欢的方式.以下代码都经过本人亲自测试. line-height: < ...

  9. MTK 系统禁止通知状态栏下拉

    之前看了网上很多修改,感觉太繁琐,修改代码太多,最后感觉还是自己找找,看能不能简单点 diff --git a/android/frameworks/base/packages/SystemUI/sr ...

  10. iphone弹出窗口效果的制作(Core animation, CALayer)

    效果类似人人网微薄客户端的弹出效果 static CGFloat kTransitionDuration = 0.3; - (void)initView { UIWindow *window = [U ...