RESOLVED - BATCH-828: org.springframework.batch.item.file.MultiResourceItemReader should allow for no resources
graceful handling for non-existing input resources
This commit is contained in:
@@ -92,6 +92,8 @@ public class FlatFileItemReader<T> extends AbstractItemReaderItemStream<T> imple
|
||||
|
||||
private LineCallbackHandler headerCallback;
|
||||
|
||||
private boolean noInput;
|
||||
|
||||
public FlatFileItemReader() {
|
||||
setName(ClassUtils.getShortName(FlatFileItemReader.class));
|
||||
}
|
||||
@@ -213,8 +215,13 @@ public class FlatFileItemReader<T> extends AbstractItemReaderItemStream<T> imple
|
||||
*/
|
||||
protected void doOpen() throws Exception {
|
||||
Assert.notNull(resource, "Input resource must not be null");
|
||||
Assert.state(resource.exists(), "Resource must exist: [" + resource + "]");
|
||||
|
||||
noInput = false;
|
||||
if (!resource.exists()) {
|
||||
noInput = true;
|
||||
log.warn("Input resource does not exist");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), encoding));
|
||||
}
|
||||
@@ -249,6 +256,9 @@ public class FlatFileItemReader<T> extends AbstractItemReaderItemStream<T> imple
|
||||
* {@link #setFieldSetMapper(FieldSetMapper)}.
|
||||
*/
|
||||
protected T doRead() throws Exception {
|
||||
if (noInput) {
|
||||
return null;
|
||||
}
|
||||
String record = readRecord();
|
||||
|
||||
if (record != null) {
|
||||
|
||||
@@ -45,7 +45,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
|
||||
private boolean saveState = true;
|
||||
|
||||
// signals there are no resources to read -> just return null on first read
|
||||
private boolean emptyInput;
|
||||
private boolean noInput;
|
||||
|
||||
private Comparator<Resource> comparator = new Comparator<Resource>() {
|
||||
|
||||
@@ -67,7 +67,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
|
||||
*/
|
||||
public T read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException {
|
||||
|
||||
if (emptyInput) {
|
||||
if (noInput) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
|
||||
public void close(ExecutionContext executionContext) throws ItemStreamException {
|
||||
index = new MultiResourceIndex();
|
||||
delegate.close(new ExecutionContext());
|
||||
emptyInput = false;
|
||||
noInput = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,10 +123,10 @@ public class MultiResourceItemReader<T> implements ItemReader<T>, ItemStream {
|
||||
|
||||
Assert.notNull(resources, "Resources must be set");
|
||||
|
||||
emptyInput = false;
|
||||
noInput = false;
|
||||
if (resources.length == 0) {
|
||||
logger.info("No resources to read");
|
||||
emptyInput = true;
|
||||
logger.warn("No resources to read");
|
||||
noInput = true;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.events.StartElement;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.file.ResourceAwareItemReaderItemStream;
|
||||
import org.springframework.batch.item.support.AbstractItemReaderItemStream;
|
||||
import org.springframework.batch.item.xml.stax.DefaultFragmentEventReader;
|
||||
@@ -35,6 +37,8 @@ import org.springframework.xml.transform.StaxSource;
|
||||
public class StaxEventItemReader<T> extends AbstractItemReaderItemStream<T> implements
|
||||
ResourceAwareItemReaderItemStream<T>, InitializingBean {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(StaxEventItemReader.class);
|
||||
|
||||
private FragmentEventReader fragmentReader;
|
||||
|
||||
private XMLEventReader eventReader;
|
||||
@@ -47,6 +51,8 @@ public class StaxEventItemReader<T> extends AbstractItemReaderItemStream<T> impl
|
||||
|
||||
private String fragmentRootElementName;
|
||||
|
||||
private boolean noInput;
|
||||
|
||||
public StaxEventItemReader() {
|
||||
setName(ClassUtils.getShortName(StaxEventItemReader.class));
|
||||
}
|
||||
@@ -137,7 +143,13 @@ public class StaxEventItemReader<T> extends AbstractItemReaderItemStream<T> impl
|
||||
|
||||
protected void doOpen() throws Exception {
|
||||
Assert.notNull(resource, "The Resource must not be null.");
|
||||
Assert.state(resource.exists(), "Input resource does not exist: [" + resource + "]");
|
||||
|
||||
noInput = false;
|
||||
if (!resource.exists()) {
|
||||
noInput = true;
|
||||
logger.warn("Input resource does not exist");
|
||||
return;
|
||||
}
|
||||
|
||||
inputStream = resource.getInputStream();
|
||||
eventReader = XMLInputFactory.newInstance().createXMLEventReader(inputStream);
|
||||
@@ -149,6 +161,11 @@ public class StaxEventItemReader<T> extends AbstractItemReaderItemStream<T> impl
|
||||
* Move to next fragment and map it to item.
|
||||
*/
|
||||
protected T doRead() throws Exception {
|
||||
|
||||
if (noInput) {
|
||||
return null;
|
||||
}
|
||||
|
||||
T item = null;
|
||||
|
||||
if (moveCursorToNextFragment(fragmentReader)) {
|
||||
|
||||
Reference in New Issue
Block a user