RESOLVED - BATCH-952: StagingItemReader is not restartable

introduced ProcessIndicatorItemWrapper (includes id) and StagingItemProcessor (marks processed inputs) to achieve restartability for parallelJob
This commit is contained in:
robokaso
2009-01-15 12:06:51 +00:00
parent 4f273334d1
commit 8e41bc5603
5 changed files with 118 additions and 20 deletions

View File

@@ -0,0 +1,39 @@
package org.springframework.batch.sample.common;
/**
* Item wrapper useful in "process indicator" usecase, where input is marked as
* processed by the processor/writer. This requires passing a technical
* identifier of the input data so that it can be modified in later stages.
*
* @param <T> item type
*
* @see StagingItemReader
* @see StagingItemProcessor
*
* @author Robert Kasanicky
*/
public class ProcessIndicatorItemWrapper<T> {
private long id;
private T item;
public ProcessIndicatorItemWrapper(long id, T item) {
this.id = id;
this.item = item;
}
/**
* @return id identifying the input data (typically row in database)
*/
public long getId() {
return id;
}
/**
* @return item (domain object for business processing)
*/
public T getItem() {
return item;
}
}

View File

@@ -0,0 +1,55 @@
package org.springframework.batch.sample.common;
import javax.sql.DataSource;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.jdbc.core.simple.SimpleJdbcOperations;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.util.Assert;
/**
* Marks the input row as 'processed'. (This change will rollback if there is
* problem later)
*
* @param <T> item type
*
* @see StagingItemReader
* @see StagingItemWriter
* @see ProcessIndicatorItemWrapper
*
* @author Robert Kasanicky
*/
public class StagingItemProcessor<T> implements ItemProcessor<ProcessIndicatorItemWrapper<T>, T>, InitializingBean {
private SimpleJdbcOperations jdbcTemplate;
public void setJdbcTemplate(SimpleJdbcOperations jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(jdbcTemplate, "Either jdbcTemplate or dataSource must be set");
}
/**
* Use the technical identifier to mark the input row as processed and
* return unwrapped item.
*/
public T process(ProcessIndicatorItemWrapper<T> wrapper) throws Exception {
int count = jdbcTemplate.update("UPDATE BATCH_STAGING SET PROCESSED=? WHERE ID=? AND PROCESSED=?",
StagingItemWriter.DONE, wrapper.getId(), StagingItemWriter.NEW);
if (count != 1) {
throw new OptimisticLockingFailureException("The staging record with ID=" + wrapper.getId()
+ " was updated concurrently when trying to mark as complete (updated " + count + " records.");
}
return wrapper.getItem();
}
}

View File

@@ -34,7 +34,6 @@ import org.springframework.batch.item.ReaderNotOpenException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.util.Assert;
@@ -42,8 +41,11 @@ import org.springframework.util.Assert;
/**
* Thread-safe database {@link ItemReader} implementing the process indicator
* pattern.
*
* To achieve restartability use together with {@link StagingItemProcessor}.
*/
public class StagingItemReader<T> implements ItemReader<T>, StepExecutionListener, InitializingBean, DisposableBean {
public class StagingItemReader<T> implements ItemReader<ProcessIndicatorItemWrapper<T>>, StepExecutionListener,
InitializingBean, DisposableBean {
private static Log logger = LogFactory.getLog(StagingItemReader.class);
@@ -90,7 +92,7 @@ public class StagingItemReader<T> implements ItemReader<T>, StepExecutionListene
}
public T read() throws DataAccessException {
public ProcessIndicatorItemWrapper<T> read() throws DataAccessException {
if (!initialized) {
throw new ReaderNotOpenException("ItemStream must be open before it can be read.");
@@ -116,15 +118,7 @@ public class StagingItemReader<T> implements ItemReader<T>, StepExecutionListene
}
}, id);
// Update now - changes will rollback if there is a problem later.
int count = jdbcTemplate.update("UPDATE BATCH_STAGING SET PROCESSED=? WHERE ID=? AND PROCESSED=?",
StagingItemWriter.DONE, id, StagingItemWriter.NEW);
if (count != 1) {
throw new OptimisticLockingFailureException("The staging record with ID=" + id
+ " was updated concurrently when trying to mark as complete (updated " + count + " records.");
}
return result;
return new ProcessIndicatorItemWrapper<T>(id, result);
}