- restore WRITE_STATISTICS_NAME from execution context on restart

- don't assume output file is always present on restart (could be
deleted by previous execution if shouldDeleteIfEmpty flag was set)
This commit is contained in:
jpraet
2013-03-19 22:23:33 +01:00
committed by Michael Minella
parent 44d640990d
commit c65ec97474
4 changed files with 97 additions and 7 deletions

View File

@@ -469,7 +469,14 @@ InitializingBean {
*/
public void restoreFrom(ExecutionContext executionContext) {
lastMarkedByteOffsetPosition = executionContext.getLong(getExecutionContextKey(RESTART_DATA_NAME));
restarted = true;
linesWritten = executionContext.getLong(getExecutionContextKey(WRITTEN_STATISTICS_NAME));
if (shouldDeleteIfEmpty && linesWritten == 0) {
// previous execution deleted the output file because no items were written
restarted = false;
lastMarkedByteOffsetPosition = 0;
} else {
restarted = true;
}
}
/**
@@ -585,7 +592,6 @@ InitializingBean {
}
initialized = true;
linesWritten = 0;
}
public boolean isInitialized() {

View File

@@ -368,6 +368,13 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
startAtPosition = executionContext.getLong(getExecutionContextKey(RESTART_DATA_NAME));
currentRecordCount = executionContext.getLong(getExecutionContextKey(WRITE_STATISTICS_NAME));
restarted = true;
if (shouldDeleteIfEmpty && currentRecordCount == 0) {
// previous execution deleted the output file because no items were written
restarted = false;
startAtPosition = 0;
} else {
restarted = true;
}
}
open(startAtPosition, restarted);

View File

@@ -323,8 +323,8 @@ public class FlatFileItemWriterTests {
assertEquals("footer", readLine());
// 3 lines were written to the file after restart
assertEquals(3, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".written"));
// 8 lines were written to the file in total
assertEquals(8, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".written"));
}
@@ -425,8 +425,8 @@ public class FlatFileItemWriterTests {
assertEquals("footer", readLine());
// 3 lines were written to the file after restart
assertEquals(3, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".written"));
// 8 lines were written to the file in total
assertEquals(8, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".written"));
}
@@ -595,6 +595,7 @@ public class FlatFileItemWriterTests {
public void testDeleteOnExitReopen() throws Exception {
writer.setShouldDeleteIfEmpty(true);
writer.open(executionContext);
writer.update(executionContext);
assertTrue(outputFile.exists());
writer.close();
assertFalse(outputFile.exists());
@@ -603,6 +604,30 @@ public class FlatFileItemWriterTests {
assertEquals("test2", readLine());
}
@Test
public void testWriteHeaderAndDeleteOnExitReopen() throws Exception {
writer.setHeaderCallback(new FlatFileHeaderCallback() {
@Override
public void writeHeader(Writer writer) throws IOException {
writer.write("a\nb");
}
});
writer.setShouldDeleteIfEmpty(true);
writer.open(executionContext);
writer.update(executionContext);
assertTrue(outputFile.exists());
writer.close();
assertFalse(outputFile.exists());
writer.open(executionContext);
writer.write(Collections.singletonList("test2"));
assertEquals("a", readLine());
assertEquals("b", readLine());
assertEquals("test2", readLine());
}
@Test
public void testDeleteOnExitNoRecordsWrittenAfterRestart() throws Exception {
writer.setShouldDeleteIfEmpty(true);

View File

@@ -443,7 +443,7 @@ public class StaxEventItemWriterTests {
* Test that the writer can restart if the previous execution deleted empty file.
*/
@Test
public void testDeleteIfEmptyRestart() throws Exception {
public void testDeleteIfEmptyRestartAfterDelete() throws Exception {
writer.setShouldDeleteIfEmpty(true);
writer.open(executionContext);
writer.update(executionContext);
@@ -452,11 +452,63 @@ public class StaxEventItemWriterTests {
writer = createItemWriter();
writer.setShouldDeleteIfEmpty(true);
writer.open(executionContext);
writer.write(items);
writer.update(executionContext);
writer.close();
String content = getOutputFileContent();
assertTrue("Wrong content: " + content, content.contains(TEST_STRING));
}
/**
* Resource is not deleted when items have been written and shouldDeleteIfEmpty flag is set (restart after delete).
*/
@Test
public void testDeleteIfEmptyNoRecordsWrittenHeaderAndFooterRestartAfterDelete() throws Exception {
writer.setShouldDeleteIfEmpty(true);
writer.setHeaderCallback(new StaxWriterCallback() {
@Override
public void write(XMLEventWriter writer) throws IOException {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "header"));
writer.add(factory.createEndElement("", "", "header"));
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
});
writer.setFooterCallback(new StaxWriterCallback() {
@Override
public void write(XMLEventWriter writer) throws IOException {
XMLEventFactory factory = XMLEventFactory.newInstance();
try {
writer.add(factory.createStartElement("", "", "footer"));
writer.add(factory.createEndElement("", "", "footer"));
}
catch (XMLStreamException e) {
throw new RuntimeException(e);
}
}
});
writer.open(executionContext);
writer.update(executionContext);
writer.close();
assertFalse("file should be deleted" + resource, resource.getFile().exists());
writer.open(executionContext);
writer.write(items);
writer.update(executionContext);
writer.close();
String content = getOutputFileContent();
assertTrue("Wrong content: " + content, content.contains(TEST_STRING));
}
/**
* Item is written to the output file with namespace.