OPEN - issue BATCH-339: ItemStream.mark() and reset() should throw a checked exception

http://jira.springframework.org/browse/BATCH-339

Make sure existing ItemStreams throw ResetFailedException where necessary.
This commit is contained in:
dsyer
2008-02-08 15:21:52 +00:00
parent 1efb6fedc9
commit c1d82aae5b
2 changed files with 29 additions and 42 deletions

View File

@@ -33,6 +33,7 @@ import org.springframework.batch.io.support.AbstractTransactionalIoSource;
import org.springframework.batch.item.ExecutionAttributes;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.KeyedItemReader;
import org.springframework.batch.item.exception.ResetFailedException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
@@ -70,11 +71,11 @@ import org.springframework.util.StringUtils;
* </p>
*
* <p>
* {@link ExecutionAttributes}: The current row is returned as restart data, and when
* restored from that same data, the cursor is opened and the current row set to
* the value within the restart data. There are also two statistics returned by
* this input source: the current line being processed and the number of lines
* that have been skipped.
* {@link ExecutionAttributes}: The current row is returned as restart data,
* and when restored from that same data, the cursor is opened and the current
* row set to the value within the restart data. There are also two statistics
* returned by this input source: the current line being processed and the
* number of lines that have been skipped.
* </p>
*
* <p>
@@ -107,8 +108,8 @@ import org.springframework.util.StringUtils;
* @author Lucas Ward
* @author Peter Zozom
*/
public class JdbcCursorItemReader extends AbstractTransactionalIoSource implements KeyedItemReader,
InitializingBean, ItemStream, Skippable {
public class JdbcCursorItemReader extends AbstractTransactionalIoSource implements KeyedItemReader, InitializingBean,
ItemStream, Skippable {
private static Log log = LogFactory.getLog(JdbcCursorItemReader.class);
@@ -241,7 +242,7 @@ public class JdbcCursorItemReader extends AbstractTransactionalIoSource implemen
*
* @throws DataAccessException
*/
public void reset() {
public void reset() throws ResetFailedException {
try {
currentProcessedRow = lastCommittedRow;
if (currentProcessedRow > 0) {
@@ -253,7 +254,8 @@ public class JdbcCursorItemReader extends AbstractTransactionalIoSource implemen
}
catch (SQLException se) {
throw getExceptionTranslator().translate("Attempted to move ResultSet to last committed row", sql, se);
throw new ResetFailedException(getExceptionTranslator().translate(
"Attempted to move ResultSet to last committed row", sql, se));
}
}
@@ -374,7 +376,8 @@ public class JdbcCursorItemReader extends AbstractTransactionalIoSource implemen
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.batch.item.stream.ItemStreamAdapter#getExecutionAttributes()
*/
public ExecutionAttributes getExecutionAttributes() {
@@ -386,7 +389,8 @@ public class JdbcCursorItemReader extends AbstractTransactionalIoSource implemen
return context;
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.batch.item.stream.ItemStreamAdapter#restoreFrom(org.springframework.batch.item.ExecutionAttributes)
*/
public void restoreFrom(ExecutionAttributes data) {

View File

@@ -34,6 +34,7 @@ import org.springframework.batch.io.support.AbstractTransactionalIoSource;
import org.springframework.batch.item.ExecutionAttributes;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.exception.ResetFailedException;
import org.springframework.batch.item.exception.StreamException;
import org.springframework.batch.item.writer.ItemTransformer;
import org.springframework.beans.factory.InitializingBean;
@@ -113,25 +114,6 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
this.resource = resource;
}
/**
* Commit the transaction.
*/
protected void transactionCommitted() {
mark();
}
/**
* Rollback the transaction.
*/
protected void transactionRolledBack() {
reset();
}
// This method removes any information in the file before this reset point.
private void resetPositionForRestart() {
getOutputState().truncate();
}
/**
* Writes out a string followed by a "new line", where the format of the new
* line separator is determined by the underlying operating system. If the
@@ -251,9 +233,7 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
public void restoreFrom(ExecutionAttributes data) {
if (data == null)
return;
getOutputState().restoreFrom(data.getProperties());
}
// Returns object representing state.
@@ -396,8 +376,8 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
fileChannel.truncate(lastMarkedByteOffsetPosition);
fileChannel.position(lastMarkedByteOffsetPosition);
}
catch (Exception e) {
throw new BatchCriticalException("An Error occured while reseting position in a file for restart", e);
catch (IOException e) {
throw new BatchCriticalException("An Error occured while truncating output file", e);
}
}
@@ -458,7 +438,7 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
// in case of restarting reset position to last commited point
if (restarted) {
this.resetPosition();
this.reset();
}
initialized = true;
@@ -497,9 +477,9 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
* truncates the file to that reset position, and set the cursor to
* start writing at that point.
*/
private void resetPosition() {
public void reset() throws BatchCriticalException {
checkFileSize();
resetPositionForRestart();
getOutputState().truncate();
}
/**
@@ -508,14 +488,14 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
* file has been damaged in some way and whole task must be started over
* again from the beginning.
*/
public void checkFileSize() {
private void checkFileSize() {
long size = -1;
try {
outputBufferedWriter.flush();
size = fileChannel.size();
}
catch (Exception e) {
catch (IOException e) {
throw new BatchCriticalException("An Error occured while checking file size", e);
}
@@ -550,8 +530,11 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
* (non-Javadoc)
* @see org.springframework.batch.io.support.AbstractTransactionalIoSource#reset(org.springframework.batch.item.ExecutionAttributes)
*/
public void reset() {
getOutputState().checkFileSize();
resetPositionForRestart();
public void reset() throws ResetFailedException {
try {
getOutputState().reset();
} catch (BatchCriticalException e) {
throw new ResetFailedException(e);
}
}
}