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.