BATCH-1957: Add StaxEventItemWriter deleteIfEmpty property
This commit is contained in:
@@ -142,6 +142,8 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
|
||||
|
||||
private boolean forceSync;
|
||||
|
||||
private boolean shouldDeleteIfEmpty = false;
|
||||
|
||||
public StaxEventItemWriter() {
|
||||
setExecutionContextName(ClassUtils.getShortName(StaxEventItemWriter.class));
|
||||
}
|
||||
@@ -203,6 +205,16 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
|
||||
this.forceSync = forceSync;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag to indicate that the target file should be deleted if no items have
|
||||
* been written (other than header and footer) on close. Defaults to false.
|
||||
*
|
||||
* @param shouldDeleteIfEmpty the flag value to set
|
||||
*/
|
||||
public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) {
|
||||
this.shouldDeleteIfEmpty = shouldDeleteIfEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get used encoding.
|
||||
*
|
||||
@@ -354,6 +366,7 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
|
||||
// otherwise start from beginning
|
||||
if (executionContext.containsKey(getExecutionContextKey(RESTART_DATA_NAME))) {
|
||||
startAtPosition = executionContext.getLong(getExecutionContextKey(RESTART_DATA_NAME));
|
||||
currentRecordCount = executionContext.getLong(getExecutionContextKey(WRITE_STATISTICS_NAME));
|
||||
restarted = true;
|
||||
}
|
||||
|
||||
@@ -656,6 +669,14 @@ ResourceAwareItemWriterItemStream<T>, InitializingBean {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (currentRecordCount == 0 && shouldDeleteIfEmpty) {
|
||||
try {
|
||||
resource.getFile().delete();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new ItemStreamException("Failed to delete empty file on close", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user