Spring Batch_JOB重启机制
在这一篇文章 对于restart做了试验,http://my.oschina.net/xinxingegeya/blog/344817在这片文章里,我们只是当job成功时,重启了job,对于job失败后,重启job有什么效果,我没有演示,下面我们就来演示一下当job失败退出后,再重启job有什么效果。
先做一个 导致job失败的情景,如下的processor :
ThrowExceptionProcessor.java
package com.lyx.batch;
import org.springframework.batch.item.ItemProcessor;
public class ThrowExceptionProcessor implements
ItemProcessor {
public PeopleDESC process(People item) throws Exception {
System.out.println("process people desc");
if ("lyx".equals(item.getFirstName())) {
throw new InvalidDataException("invalid data");
}
return new PeopleDESC(item.getLastName(), item.getFirstName(), Thread
.currentThread().getName());
}
}
当判断某条数据符合失败条件后,抛出异常 ,导致job失败。
下面是整个配置文件:spring-batch-failure-restart.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
class="org.springframework.batch.item.database.JdbcCursorItemReader">
start-limit="3">
writer="addDescPeopleWriter" commit-interval="2" />
scope="step">
first_name like ? or last_name like ?]]>
destroy-method="close">
data-source="dataSource" transaction-manager="transactionManager"
isolation-level-for-create="REPEATABLE_READ" table-prefix="BATCH_"
max-varchar-length="1000" />
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
运行任务:
AppMain13.java
package com.lyx.batch;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.

本文详细探讨了Spring Batch中作业(JOB)在失败后如何重启,并通过示例展示了重启作业时Spring Batch如何处理数据,确保数据完整性。通过分析不同运行阶段的数据库记录,解释了Spring Batch在restart时是从失败的事务边界内第一条记录开始重复执行的。
最低0.47元/天 解锁文章
3999

被折叠的 条评论
为什么被折叠?



