OPEN - issue BATCH-364: StepScope responsibilities can be assumed by Step (not ApplicationContext)
http://jira.springframework.org/browse/BATCH-364 Instead of lazy open, throw exception is stream is used before open().
This commit is contained in:
@@ -135,7 +135,7 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
((AbstractLineTokenizer) tokenizer).setNames(names);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
mark();
|
||||
}
|
||||
|
||||
@@ -180,9 +180,9 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
}
|
||||
|
||||
/**
|
||||
* This method initialises the reader for Restart. It opens the input
|
||||
* file and position the buffer reader according to information provided by
|
||||
* the restart data
|
||||
* This method initialises the reader for Restart. It opens the input file
|
||||
* and position the buffer reader according to information provided by the
|
||||
* restart data
|
||||
*
|
||||
* @param data {@link ExecutionAttributes} information
|
||||
*/
|
||||
@@ -209,8 +209,8 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
}
|
||||
|
||||
/**
|
||||
* This method returns the execution attributes for the reader. It returns the
|
||||
* current Line Count which can be used to reinitialise the batch job in
|
||||
* This method returns the execution attributes for the reader. It returns
|
||||
* the current Line Count which can be used to reinitialise the batch job in
|
||||
* case of restart.
|
||||
*/
|
||||
public ExecutionAttributes getExecutionAttributes() {
|
||||
@@ -262,7 +262,8 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
}
|
||||
|
||||
/**
|
||||
* @return next line to be tokenized and mapped (possibly skips multiple lines).
|
||||
* @return next line to be tokenized and mapped (possibly skips multiple
|
||||
* lines).
|
||||
*/
|
||||
protected String readLine() {
|
||||
String line = nextLine();
|
||||
@@ -280,6 +281,9 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
try {
|
||||
return (String) getReader().read();
|
||||
}
|
||||
catch (StreamException e) {
|
||||
throw e;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
@@ -290,7 +294,7 @@ public class FlatFileItemReader implements ItemReader, Skippable, ItemStream, In
|
||||
*/
|
||||
protected LineReader getReader() {
|
||||
if (reader == null) {
|
||||
open();
|
||||
throw new StreamException("ItemStream must be open before it can be read.");
|
||||
// reader is now not null, or else an exception is thrown
|
||||
}
|
||||
return reader;
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.springframework.batch.io.xml.stax.TransactionalEventReader;
|
||||
import org.springframework.batch.item.ExecutionAttributes;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.item.exception.StreamException;
|
||||
import org.springframework.batch.item.reader.AbstractItemReader;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -68,7 +69,7 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
*/
|
||||
public Object read() {
|
||||
if (!initialized) {
|
||||
open();
|
||||
throw new StreamException("ItemStream must be open before it can be read.");
|
||||
}
|
||||
Object item = null;
|
||||
|
||||
@@ -192,13 +193,11 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
* available records.
|
||||
*/
|
||||
public void restoreFrom(ExecutionAttributes data) {
|
||||
Assert.state(!initialized);
|
||||
|
||||
if (data == null || data.getProperties() == null || !data.containsKey(READ_COUNT_STATISTICS_NAME)) {
|
||||
return;
|
||||
}
|
||||
|
||||
open();
|
||||
|
||||
long restoredRecordCount = data.getLong(READ_COUNT_STATISTICS_NAME);
|
||||
int REASONABLE_ADHOC_COMMIT_FREQUENCY = 100;
|
||||
while (currentRecordCount <= restoredRecordCount) {
|
||||
@@ -206,11 +205,14 @@ public class StaxEventItemReader extends AbstractItemReader implements ItemReade
|
||||
if (currentRecordCount % REASONABLE_ADHOC_COMMIT_FREQUENCY == 0) {
|
||||
txReader.onCommit(); // reset the history buffer
|
||||
}
|
||||
Assert.state(fragmentReader.hasNext(), "restore point must be before end of input");
|
||||
if (!fragmentReader.hasNext()) {
|
||||
throw new StreamException("Restore point must be before end of input");
|
||||
}
|
||||
fragmentReader.next();
|
||||
moveCursorToNextFragment(fragmentReader);
|
||||
}
|
||||
mark(); // reset the history buffer
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -342,7 +342,7 @@ public class StaxEventItemWriter implements ItemWriter, ItemStream, Initializing
|
||||
public void write(Object output) {
|
||||
|
||||
if (!initialized) {
|
||||
open();
|
||||
throw new StreamException("ItemStream must be open before it can be used.");
|
||||
}
|
||||
|
||||
currentRecordCount++;
|
||||
|
||||
@@ -157,6 +157,7 @@ public class FlatFileItemReaderAdvancedTests extends TestCase {
|
||||
} catch (StreamException e) {
|
||||
assertTrue("Message does not contain open: "+e.getMessage(), e.getMessage().contains("open"));
|
||||
}
|
||||
reader.open();
|
||||
assertEquals("[FlatFileInputTemplate-TestData]", reader.read().toString());
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.springframework.batch.io.file.mapping.FieldSetMapper;
|
||||
import org.springframework.batch.io.file.separator.DefaultRecordSeparatorPolicy;
|
||||
import org.springframework.batch.io.file.transform.DelimitedLineTokenizer;
|
||||
import org.springframework.batch.io.file.transform.LineTokenizer;
|
||||
import org.springframework.batch.item.exception.StreamException;
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -139,7 +140,12 @@ public class FlatFileItemReaderBasicTests extends TestCase {
|
||||
itemReader = new FlatFileItemReader();
|
||||
itemReader.setResource(getInputResource(TEST_STRING));
|
||||
itemReader.setFieldSetMapper(fieldSetMapper);
|
||||
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());
|
||||
try {
|
||||
itemReader.read();
|
||||
fail("Expected StreamException");
|
||||
} catch (StreamException e) {
|
||||
assertTrue(e.getMessage().contains("open"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testCloseBeforeOpen() throws Exception {
|
||||
@@ -147,7 +153,8 @@ public class FlatFileItemReaderBasicTests extends TestCase {
|
||||
itemReader.setResource(getInputResource(TEST_STRING));
|
||||
itemReader.setFieldSetMapper(fieldSetMapper);
|
||||
itemReader.close();
|
||||
// The open still happens automatically on a read...
|
||||
// The open does not happen automatically on a read...
|
||||
itemReader.open();
|
||||
assertEquals("[FlatFileInputTemplate-TestData]", itemReader.read().toString());
|
||||
}
|
||||
|
||||
@@ -172,6 +179,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
|
||||
itemReader.setEncoding("UTF-8");
|
||||
itemReader.setResource(getInputResource(TEST_STRING));
|
||||
itemReader.setFieldSetMapper(fieldSetMapper);
|
||||
itemReader.open();
|
||||
testRead();
|
||||
}
|
||||
|
||||
@@ -301,6 +309,7 @@ public class FlatFileItemReaderBasicTests extends TestCase {
|
||||
|
||||
//replace the resource to simulate runtime resource creation
|
||||
testReader.setResource(getInputResource(TEST_STRING));
|
||||
testReader.open();
|
||||
assertEquals(TEST_OUTPUT, testReader.read().toString());
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import javax.xml.stream.events.XMLEvent;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.batch.item.ExecutionAttributes;
|
||||
import org.springframework.batch.item.exception.StreamException;
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
@@ -131,23 +132,19 @@ public class StaxEventItemReaderTests extends TestCase {
|
||||
context.putLong(StaxEventItemReader.READ_COUNT_STATISTICS_NAME, 100000);
|
||||
try {
|
||||
source.restoreFrom(context);
|
||||
fail();
|
||||
fail("Expected StreamException");
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
source = createNewInputSouce();
|
||||
source.open();
|
||||
try {
|
||||
source.restoreFrom(new ExecutionAttributes());
|
||||
fail();
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
catch (StreamException e) {
|
||||
// expected
|
||||
String message = e.getMessage();
|
||||
assertTrue("Wrong message: "+message, message.contains("must be before"));
|
||||
}
|
||||
}
|
||||
|
||||
public void testRestoreWorksFromClosedStream() throws Exception {
|
||||
source.close();
|
||||
source.restoreFrom(new ExecutionAttributes());
|
||||
}
|
||||
/**
|
||||
* Skipping marked records after rollback.
|
||||
*/
|
||||
@@ -224,16 +221,22 @@ public class StaxEventItemReaderTests extends TestCase {
|
||||
newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
|
||||
newSource.setFragmentDeserializer(deserializer);
|
||||
|
||||
newSource.open();
|
||||
|
||||
Object item = newSource.read();
|
||||
assertNotNull(item);
|
||||
assertTrue(newSource.isOpenCalled());
|
||||
|
||||
newSource.close();
|
||||
newSource.close();
|
||||
newSource.setOpenCalled(false);
|
||||
// calling read again should require re-initialization because of close
|
||||
item = newSource.read();
|
||||
assertNotNull(item);
|
||||
assertTrue(newSource.isOpenCalled());
|
||||
try {
|
||||
item = newSource.read();
|
||||
fail("Expected StreamException");
|
||||
}
|
||||
catch (StreamException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testOpenBadIOInput() {
|
||||
@@ -293,6 +296,8 @@ public class StaxEventItemReaderTests extends TestCase {
|
||||
newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
|
||||
newSource.setFragmentDeserializer(deserializer);
|
||||
|
||||
newSource.open();
|
||||
|
||||
return newSource;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
protected void setUp() throws Exception {
|
||||
resource = new FileSystemResource(File.createTempFile("StaxEventWriterOutputSourceTests", "xml"));
|
||||
writer = createItemWriter();
|
||||
writer.open();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,17 +85,18 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
* Restart scenario - content is appended to the output file after restart.
|
||||
*/
|
||||
public void testRestart() throws Exception {
|
||||
// write records
|
||||
// write record
|
||||
writer.write(record);
|
||||
writer.mark();
|
||||
// writer.mark();
|
||||
ExecutionAttributes streamContext = writer.getExecutionAttributes();
|
||||
writer.close();
|
||||
|
||||
// create new writer from saved restart data and continue writing
|
||||
writer = createItemWriter();
|
||||
writer.restoreFrom(streamContext);
|
||||
writer.write(record);
|
||||
writer.close();
|
||||
|
||||
|
||||
// check the output is concatenation of 'before restart' and 'after
|
||||
// restart' writes.
|
||||
String outputFile = outputFileContent();
|
||||
@@ -199,7 +201,7 @@ public class StaxEventWriterItemWriterTests extends TestCase {
|
||||
source.setOverwriteOutput(true);
|
||||
|
||||
source.afterPropertiesSet();
|
||||
|
||||
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,7 @@ public abstract class AbstractTradeBatchTests extends TestCase {
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
provider = new TradeItemReader(resource);
|
||||
provider.open();
|
||||
}
|
||||
|
||||
protected static class TradeItemReader extends DelegatingItemReader {
|
||||
|
||||
Reference in New Issue
Block a user