BATCH-493: Added saveState flag to FlatFileItemWriter and FlatFileItemReader.
This commit is contained in:
@@ -94,6 +94,8 @@ public class FlatFileItemReader extends ExecutionContextUserSupport implements I
|
||||
private LineTokenizer tokenizer = new DelimitedLineTokenizer();
|
||||
|
||||
private FieldSetMapper fieldSetMapper;
|
||||
|
||||
private boolean saveState = false;
|
||||
|
||||
/**
|
||||
* Encapsulates the state of the input source. If it is null then we are uninitialized.
|
||||
@@ -203,9 +205,12 @@ public class FlatFileItemReader extends ExecutionContextUserSupport implements I
|
||||
if (reader == null) {
|
||||
throw new ItemStreamException("ItemStream not open or already closed.");
|
||||
}
|
||||
Assert.notNull(executionContext, "ExecutionContext must not be null");
|
||||
executionContext.putLong(getKey(LINES_READ_COUNT), reader.getPosition());
|
||||
executionContext.putLong(getKey(SKIPPED_STATISTICS_NAME), skippedLines.size());
|
||||
|
||||
if(saveState){
|
||||
Assert.notNull(executionContext, "ExecutionContext must not be null");
|
||||
executionContext.putLong(getKey(LINES_READ_COUNT), reader.getPosition());
|
||||
executionContext.putLong(getKey(SKIPPED_STATISTICS_NAME), skippedLines.size());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -357,5 +362,17 @@ public class FlatFileItemReader extends ExecutionContextUserSupport implements I
|
||||
Assert.notNull(resource, "Input resource must not be null");
|
||||
Assert.notNull(fieldSetMapper, "FieldSetMapper must not be null.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the boolean indicating whether or not state should be saved
|
||||
* in the provided {@link ExecutionContext} during the {@link ItemStream}
|
||||
* call to update. Setting this to false means that it will always start
|
||||
* at the beginning.
|
||||
*
|
||||
* @param saveState
|
||||
*/
|
||||
public void setSaveState(boolean saveState) {
|
||||
this.saveState = saveState;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -80,6 +80,8 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
|
||||
private LineAggregator lineAggregator = new DelimitedLineAggregator();
|
||||
|
||||
private FieldSetCreator fieldSetCreator;
|
||||
|
||||
private boolean saveState = false;
|
||||
|
||||
public FlatFileItemWriter() {
|
||||
setName(ClassUtils.getShortName(FlatFileItemWriter.class));
|
||||
@@ -191,16 +193,21 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
|
||||
if (state == null) {
|
||||
throw new ItemStreamException("ItemStream not open or already closed.");
|
||||
}
|
||||
|
||||
|
||||
Assert.notNull(executionContext, "ExecutionContext must not be null");
|
||||
|
||||
try {
|
||||
executionContext.putLong(getKey(RESTART_DATA_NAME), state.position());
|
||||
} catch (IOException e) {
|
||||
throw new ItemStreamException("ItemStream does not return current position properly", e);
|
||||
|
||||
if(saveState){
|
||||
|
||||
try {
|
||||
executionContext.putLong(getKey(RESTART_DATA_NAME), state.position());
|
||||
} catch (IOException e) {
|
||||
throw new ItemStreamException("ItemStream does not return current position properly", e);
|
||||
}
|
||||
|
||||
executionContext.putLong(getKey(WRITTEN_STATISTICS_NAME), state.linesWritten);
|
||||
executionContext.putLong(getKey(RESTART_COUNT_STATISTICS_NAME), state.restartCount);
|
||||
}
|
||||
|
||||
executionContext.putLong(getKey(WRITTEN_STATISTICS_NAME), state.linesWritten);
|
||||
executionContext.putLong(getKey(RESTART_COUNT_STATISTICS_NAME), state.restartCount);
|
||||
}
|
||||
|
||||
// Returns object representing state.
|
||||
@@ -467,5 +474,17 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
|
||||
public void flush() throws FlushFailedException {
|
||||
getOutputState().mark();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the boolean indicating whether or not state should be saved
|
||||
* in the provided {@link ExecutionContext} during the {@link ItemStream}
|
||||
* call to update. Setting this to false means that it will always start
|
||||
* at the beginning.
|
||||
*
|
||||
* @param saveState
|
||||
*/
|
||||
public void setSaveState(boolean saveState) {
|
||||
this.saveState = saveState;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -65,6 +65,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
|
||||
reader.setResource(getInputResource(TEST_STRING));
|
||||
reader.setLineTokenizer(tokenizer);
|
||||
reader.setFieldSetMapper(fieldSetMapper);
|
||||
reader.setSaveState(true);
|
||||
// context argument is necessary only for the FileLocator, which
|
||||
// is mocked
|
||||
executionContext = new ExecutionContext();
|
||||
|
||||
@@ -71,6 +71,7 @@ public class FlatFileItemWriterTests extends TestCase {
|
||||
inputSource.setResource(new FileSystemResource(outputFile));
|
||||
inputSource.setFieldSetCreator(new PassThroughFieldSetMapper());
|
||||
inputSource.afterPropertiesSet();
|
||||
inputSource.setSaveState(true);
|
||||
executionContext = new ExecutionContext();
|
||||
}
|
||||
|
||||
@@ -267,6 +268,7 @@ public class FlatFileItemWriterTests extends TestCase {
|
||||
inputSource.setResource(new FileSystemResource(outputFile));
|
||||
inputSource.setFieldSetCreator(new PassThroughFieldSetMapper());
|
||||
inputSource.afterPropertiesSet();
|
||||
inputSource.setSaveState(true);
|
||||
inputSource.open(executionContext);
|
||||
inputSource.update(executionContext);
|
||||
assertNotNull(executionContext);
|
||||
|
||||
Reference in New Issue
Block a user