OPEN - issue BATCH-788: Remove flush/clear from ItemWriter
Infrastructure tests fixed
This commit is contained in:
@@ -4,65 +4,66 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Utility methods for files used in batch processing.
|
||||
*
|
||||
*
|
||||
* @author Peter Zozom
|
||||
*/
|
||||
public class FileUtils {
|
||||
|
||||
// forbids instantiation
|
||||
private FileUtils() {}
|
||||
private FileUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up output file for batch processing. This method implements common logic for
|
||||
* handling output files when starting or restarting job/step.
|
||||
* When starting output file processing, method creates/overwrites new file.
|
||||
* When restarting output file processing, method checks whether file is writable.
|
||||
*
|
||||
* Set up output file for batch processing. This method implements common
|
||||
* logic for handling output files when starting or restarting file I/O.
|
||||
* When starting output file processing, creates/overwrites new file. When
|
||||
* restarting output file processing, checks whether file is writable.
|
||||
*
|
||||
* @param file file to be set up
|
||||
* @param restarted TRUE signalizes that we are restarting output file processing
|
||||
* @param overwriteOutputFile If set to TRUE, output file will be overwritten
|
||||
* (this flag is ignored when processing is restart)
|
||||
*
|
||||
* @throws IllegalArgumentException when file is NULL
|
||||
* @throws IllegalStateException when staring output file processing, file exists and
|
||||
* flag "shouldDeleteExisting" is set to FALSE
|
||||
* @throws DataAccessResourceFailureException when unable to create file or file is not writable
|
||||
* @param restarted true signals that we are restarting output file
|
||||
* processing
|
||||
* @param overwriteOutputFile If set to true, output file will be
|
||||
* overwritten (this flag is ignored when processing is restart)
|
||||
*
|
||||
* @throws IllegalArgumentException when file is null
|
||||
* @throws ItemStreamException when starting output file processing, file
|
||||
* exists and flag "overwriteOutputFile" is set to false
|
||||
* @throws ItemStreamException when unable to create file or file is not
|
||||
* writable
|
||||
*/
|
||||
public static void setUpOutputFile(File file, boolean restarted,
|
||||
boolean overwriteOutputFile) {
|
||||
public static void setUpOutputFile(File file, boolean restarted, boolean overwriteOutputFile) {
|
||||
|
||||
Assert.notNull(file);
|
||||
|
||||
try {
|
||||
if (!restarted) {
|
||||
if (file.exists()) {
|
||||
if(!overwriteOutputFile){
|
||||
throw new ItemStreamException("File already exists: ["
|
||||
+ file.getAbsolutePath() + "]");
|
||||
if (!overwriteOutputFile) {
|
||||
throw new ItemStreamException("File already exists: [" + file.getAbsolutePath() + "]");
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
|
||||
if (file.getParent() != null ) {
|
||||
if (file.getParent() != null) {
|
||||
new File(file.getParent()).mkdirs();
|
||||
}
|
||||
file.createNewFile();
|
||||
Assert.state(file.exists(), "Output file must exist");
|
||||
if (!file.exists()) {
|
||||
throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath()
|
||||
+ "]");
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
throw new ItemStreamException(
|
||||
"Unable to create file: [" + file.getAbsolutePath() + "]",
|
||||
ioe);
|
||||
}
|
||||
catch (IOException ioe) {
|
||||
throw new ItemStreamException("Unable to create file: [" + file.getAbsolutePath() + "]", ioe);
|
||||
}
|
||||
|
||||
if (!file.canWrite()) {
|
||||
throw new ItemStreamException(
|
||||
"File is not writable: [" + file.getAbsolutePath() + "]");
|
||||
throw new ItemStreamException("File is not writable: [" + file.getAbsolutePath() + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,9 +106,6 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
// XML event writer
|
||||
private XMLEventWriter delegateEventWriter;
|
||||
|
||||
// byte offset in file channel at last commit point
|
||||
private long lastCommitPointPosition = 0;
|
||||
|
||||
// current count of processed records
|
||||
private long currentRecordCount = 0;
|
||||
|
||||
@@ -412,7 +409,6 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
throw new FlushFailedException("Failed to flush the events", e);
|
||||
}
|
||||
|
||||
lastCommitPointPosition = getPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -458,8 +454,6 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
private void setPosition(long newPosition) {
|
||||
|
||||
try {
|
||||
Assert.state(channel.size() >= lastCommitPointPosition,
|
||||
"Current file size is smaller than size at last commit");
|
||||
channel.truncate(newPosition);
|
||||
channel.position(newPosition);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user