OPEN - issue BATCH-364: StepScope responsibilities can be assumed by Step (not ApplicationContext)
http://jira.springframework.org/browse/BATCH-364 Instead of lazy open, throw exception is stream is used before open().
This commit is contained in:
@@ -135,7 +135,7 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
((AbstractLineTokenizer) tokenizer).setNames(names);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
mark();
|
||||
}
|
||||
|
||||
@@ -180,9 +180,9 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
}
|
||||
|
||||
/**
|
||||
* This method initialises the reader for Restart. It opens the input
|
||||
* file and position the buffer reader according to information provided by
|
||||
* the restart data
|
||||
* This method initialises the reader for Restart. It opens the input file
|
||||
* and position the buffer reader according to information provided by the
|
||||
* restart data
|
||||
*
|
||||
* @param data {@link ExecutionAttributes} information
|
||||
*/
|
||||
@@ -209,8 +209,8 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the execution attributes for the reader. It returns the
|
||||
* current Line Count which can be used to reinitialise the batch job in
|
||||
* This method returns the execution attributes for the reader. It returns
|
||||
* the current Line Count which can be used to reinitialise the batch job in
|
||||
* case of restart.
|
||||
*/
|
||||
public ExecutionAttributes getExecutionAttributes() {
|
||||
@@ -262,7 +262,8 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
}
|
||||
|
||||
/**
|
||||
* @return next line to be tokenized and mapped (possibly skips multiple lines).
|
||||
* @return next line to be tokenized and mapped (possibly skips multiple
|
||||
* lines).
|
||||
*/
|
||||
protected String readLine() {
|
||||
String line = nextLine();
|
||||
@@ -280,6 +281,9 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
try {
|
||||
return (String) getReader().read();
|
||||
}
|
||||
catch (StreamException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
@@ -290,7 +294,7 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
*/
|
||||
protected LineReader getReader() {
|
||||
if (reader == null) {
|
||||
open();
|
||||
throw new StreamException("ItemStream must be open before it can be read.");
|
||||
// reader is now not null, or else an exception is thrown
|
||||
}
|
||||
return reader;
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.springframework.batch.io.xml.stax.TransactionalEventReader;
|
||||
import org.springframework.batch.item.ExecutionAttributes;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.exception.StreamException;
|
||||
import org.springframework.batch.item.reader.AbstractItemReader;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -68,7 +69,7 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
*/
|
||||
public Object read() {
|
||||
if (!initialized) {
|
||||
open();
|
||||
throw new StreamException("ItemStream must be open before it can be read.");
|
||||
}
|
||||
Object item = null;
|
||||
|
||||
@@ -192,13 +193,11 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
* available records.
|
||||
*/
|
||||
public void restoreFrom(ExecutionAttributes data) {
|
||||
Assert.state(!initialized);
|
||||
|
||||
if (data == null || data.getProperties() == null || !data.containsKey(READ_COUNT_STATISTICS_NAME)) {
|
||||
return;
|
||||
}
|
||||
|
||||
open();
|
||||
|
||||
long restoredRecordCount = data.getLong(READ_COUNT_STATISTICS_NAME);
|
||||
int REASONABLE_ADHOC_COMMIT_FREQUENCY = 100;
|
||||
while (currentRecordCount <= restoredRecordCount) {
|
||||
@@ -206,11 +205,14 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
if (currentRecordCount % REASONABLE_ADHOC_COMMIT_FREQUENCY == 0) {
|
||||
txReader.onCommit(); // reset the history buffer
|
||||
}
|
||||
Assert.state(fragmentReader.hasNext(), "restore point must be before end of input");
|
||||
if (!fragmentReader.hasNext()) {
|
||||
throw new StreamException("Restore point must be before end of input");
|
||||
}
|
||||
fragmentReader.next();
|
||||
moveCursorToNextFragment(fragmentReader);
|
||||
}
|
||||
mark(); // reset the history buffer
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -342,7 +342,7 @@ public class StaxEventItemWriter implements ItemWriter, ItemStream, Initializing
|
||||
public void write(Object output) {
|
||||
|
||||
if (!initialized) {
|
||||
open();
|
||||
throw new StreamException("ItemStream must be open before it can be used.");
|
||||
}
|
||||
|
||||
currentRecordCount++;
|
||||
|
||||
Reference in New Issue
Block a user