BATCH-1603: change item readers so they can still read after open fails

This commit is contained in:
dsyer
2010-08-03 06:25:30 +00:00
parent df1783a76e
commit 94d4437555
6 changed files with 97 additions and 26 deletions

View File

@@ -179,17 +179,17 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
}
String line = readLine();
if (line == null) {
return null;
}
else {
try{
try {
return lineMapper.mapLine(line, lineCount);
}
catch(Exception ex){
logger.error("Parsing error at line: " + lineCount + " in resource=" +
resource.getDescription() + ", input=[" + line + "]", ex);
catch (Exception ex) {
logger.error("Parsing error at line: " + lineCount + " in resource=" + resource.getDescription()
+ ", input=[" + line + "]", ex);
throw ex;
}
}
@@ -219,7 +219,7 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
}
lineCount++;
}
line = applyRecordSeparatorPolicy(line);
}
catch (IOException e) {
@@ -250,21 +250,20 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
Assert.notNull(resource, "Input resource must be set");
Assert.notNull(recordSeparatorPolicy, "RecordSeparatorPolicy must be set");
noInput = false;
noInput = true;
if (!resource.exists()) {
if (strict) {
throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode): " + resource);
}
noInput = true;
logger.warn("Input resource does not exist " + resource.getDescription());
return;
}
if (!resource.isReadable()) {
if (strict) {
throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode): " + resource);
throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode): "
+ resource);
}
noInput = true;
logger.warn("Input resource is not readable " + resource.getDescription());
return;
}
@@ -276,21 +275,22 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
skippedLinesCallback.handleLine(line);
}
}
noInput = false;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(lineMapper, "LineMapper is required");
}
@Override
protected void jumpToItem(int itemIndex) throws Exception {
for (int i = 0; i < itemIndex; i++) {
readLine();
}
}
private String applyRecordSeparatorPolicy(String line) throws IOException{
private String applyRecordSeparatorPolicy(String line) throws IOException {
String record = line;
while (line != null && !recordSeparatorPolicy.isEndOfRecord(record)) {
line = this.reader.readLine();
@@ -302,18 +302,19 @@ public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemRea
}
else {
// Record has no text but it might still be post processed
// to something (skipping preProcess since that was already done)
// to something (skipping preProcess since that was already
// done)
break;
}
} else {
}
else {
lineCount++;
}
}
record = recordSeparatorPolicy.preProcess(record) + line;
}
return recordSeparatorPolicy.postProcess(record);
}
}

View File

@@ -33,7 +33,6 @@ import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.WriteFailedException;
import org.springframework.batch.item.WriterNotOpenException;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.transform.LineAggregator;
import org.springframework.batch.item.util.ExecutionContextUserSupport;
import org.springframework.batch.item.util.FileUtils;

View File

@@ -148,7 +148,8 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
}
QName startElementName = ((StartElement) reader.peek()).getName();
if (startElementName.getLocalPart().equals(fragmentRootElementName)) {
if (fragmentRootElementNameSpace==null || startElementName.getNamespaceURI().equals(fragmentRootElementNameSpace)) {
if (fragmentRootElementNameSpace == null
|| startElementName.getNamespaceURI().equals(fragmentRootElementNameSpace)) {
return true;
}
}
@@ -180,12 +181,11 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
protected void doOpen() throws Exception {
Assert.notNull(resource, "The Resource must not be null.");
noInput = false;
noInput = true;
if (!resource.exists()) {
if (strict) {
throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)");
}
noInput = true;
logger.warn("Input resource does not exist " + resource.getDescription());
return;
}
@@ -193,7 +193,6 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
if (strict) {
throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode)");
}
noInput = true;
logger.warn("Input resource is not readable " + resource.getDescription());
return;
}
@@ -201,6 +200,7 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
inputStream = resource.getInputStream();
eventReader = XMLInputFactory.newInstance().createXMLEventReader(inputStream);
fragmentReader = new DefaultFragmentEventReader(eventReader);
noInput = false;
}