batch spring 重复执行_Spring Batch_JOB重启机制

本文详细探讨了Spring Batch中作业(JOB)在失败后如何重启,并通过示例展示了重启作业时Spring Batch如何处理数据,确保数据完整性。通过分析不同运行阶段的数据库记录,解释了Spring Batch在restart时是从失败的事务边界内第一条记录开始重复执行的。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值