From 300e726909d875f1a9e3d68f11299bb95f1433f7 Mon Sep 17 00:00:00 2001 From: dsyer Date: Thu, 21 Aug 2008 11:38:00 +0000 Subject: [PATCH] OPEN - issue BATCH-789: Remove mark/reset from ItemReader --- .../batch/item/ItemReader.java | 29 ------------------- .../batch/item/adapter/ItemReaderAdapter.java | 13 --------- .../item/database/DrivingQueryItemReader.java | 6 ---- .../database/HibernateCursorItemReader.java | 16 +++++----- .../item/database/JdbcCursorItemReader.java | 23 +++++++-------- .../item/database/JpaPagingItemReader.java | 9 +----- .../item/file/MultiResourceItemReader.java | 15 ---------- .../item/support/AbstractItemReader.java | 7 ----- .../support/AbstractItemReaderItemStream.java | 12 -------- .../item/support/AggregateItemReader.java | 8 ----- ...bstractJdbcItemReaderIntegrationTests.java | 17 ++++++----- .../batch/item/database/FooInputSource.java | 13 --------- .../file/FlatFileItemReaderAdvancedTests.java | 29 ++++++++----------- .../support/DelegatingItemReaderTests.java | 6 ---- .../sample/common/CustomItemReaderTests.java | 27 ++--------------- 15 files changed, 43 insertions(+), 187 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemReader.java index a9cd37286..19c93f2b5 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/ItemReader.java @@ -48,33 +48,4 @@ public interface ItemReader { */ T read() throws Exception, UnexpectedInputException, NoWorkFoundException, ParseException; - /** - * Mark the stream so that it can be reset later and the items backed - * out.
- * - * Mark is called before reading a new chunk of items - in case of rollback - * mark will not be called again before re-processing the chunk.
- * - * @throws MarkFailedException if there is a problem with the mark. If a - * mark fails inside a transaction, it would be worrying, but not normally - * fatal. - * - * @deprecated - */ - void mark() throws MarkFailedException; - - /** - * Reset the stream to the last mark. After a reset the stream state will be - * such that changes (items read or written) since the last call to mark - * will not be visible after a call to close.
- * - * @throws ResetFailedException if there is a problem with the reset. If a - * reset fails inside a transaction, it would normally be fatal, and would - * leave the stream in an inconsistent state. So while this is an unchecked - * exception, it may be important for a client to catch it explicitly. - * - * @deprecated - */ - void reset() throws ResetFailedException; - } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/adapter/ItemReaderAdapter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/adapter/ItemReaderAdapter.java index a6fb858fc..3f8031ac8 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/adapter/ItemReaderAdapter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/adapter/ItemReaderAdapter.java @@ -17,8 +17,6 @@ package org.springframework.batch.item.adapter; import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.MarkFailedException; -import org.springframework.batch.item.ResetFailedException; /** * Invokes a custom method on a delegate plain old Java object which itself @@ -35,15 +33,4 @@ public class ItemReaderAdapter extends AbstractMethodInvokingDelegator imp return invokeDelegateMethod(); } - /** - * No-op. - */ - public void mark() throws MarkFailedException { - } - - /** - * No-op. - */ - public void reset() throws ResetFailedException { - } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/DrivingQueryItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/DrivingQueryItemReader.java index a7a393829..0a72b064b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/DrivingQueryItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/DrivingQueryItemReader.java @@ -169,12 +169,6 @@ public class DrivingQueryItemReader implements ItemReader, InitializingBea this.keyCollector = keyCollector; } - public void mark() { - } - - public void reset() { - } - public void setSaveState(boolean saveState) { this.saveState = saveState; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateCursorItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateCursorItemReader.java index c18853cbb..6f70942cb 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateCursorItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/HibernateCursorItemReader.java @@ -21,8 +21,10 @@ import org.hibernate.ScrollableResults; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.StatelessSession; +import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; +import org.springframework.batch.item.ItemStreamException; import org.springframework.batch.item.support.AbstractItemReaderItemStream; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; @@ -121,15 +123,11 @@ public class HibernateCursorItemReader extends AbstractItemReaderItemStream extends AbstractItemReaderItemStream * Transactions: The same ResultSet is held open regardless of commits or roll - * backs in a surrounding transaction. This means that when such a transaction - * is committed, the reader is notified through the {@link #mark()} and - * {@link #reset()} so that it can save it's current row number. Later, if the - * transaction is rolled back, the current row can be moved back to the same row - * number as it was on when commit was called. + * backs in a surrounding transaction. When a transaction is committed, the + * reader will be notified through the {@link #update(ExecutionContext)} so that + * it can save it's current row number. Clients of this reader are responsible + * for buffering the items in the case that they need to be re-presented on a + * rollback. *

* *

@@ -259,7 +259,7 @@ public class JdbcCursorItemReader extends AbstractItemReaderItemStream imp private void moveCursorToRow(int row) { try { int count = 0; - while (row!=count && rs.next()) { + while (row != count && rs.next()) { count++; } } @@ -294,9 +294,8 @@ public class JdbcCursorItemReader extends AbstractItemReaderItemStream imp /** * Sets the number of seconds the driver will wait for a - * Statement object to execute to the given number of - * seconds. If the limit is exceeded, an SQLException is - * thrown. + * Statement object to execute to the given number of seconds. + * If the limit is exceeded, an SQLException is thrown. * * @param queryTimeout seconds the new query timeout limit in seconds; zero * means there is no limit @@ -359,9 +358,9 @@ public class JdbcCursorItemReader extends AbstractItemReaderItemStream imp /** * Indicate whether the JDBC driver supports setting the absolute row on a * {@link ResultSet}. It is recommended that this is set to - * true for JDBC drivers that supports ResultSet.absolute() - * as it may improve performance, especially if a step fails while working - * with a large data set. + * true for JDBC drivers that supports ResultSet.absolute() as + * it may improve performance, especially if a step fails while working with + * a large data set. * * @see ResultSet#absolute(int) * diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaPagingItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaPagingItemReader.java index 337dc60a1..9db5f6edc 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaPagingItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JpaPagingItemReader.java @@ -99,15 +99,8 @@ public class JpaPagingItemReader extends AbstractItemReaderItemStream impl this.queryString = queryString; } - @Override - public void mark() { - - super.mark(); - - } - /** - * The number of entities to retreive at a time. + * The number of entities to retrieve at a time. * * @param pageSize the number of rows to fetch, 10 by default * @see javax.persistence.Query#setMaxResults(int) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java index 7d521df39..aed273990 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/MultiResourceItemReader.java @@ -7,10 +7,8 @@ import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.ItemStreamException; -import org.springframework.batch.item.MarkFailedException; import org.springframework.batch.item.NoWorkFoundException; import org.springframework.batch.item.ParseException; -import org.springframework.batch.item.ResetFailedException; import org.springframework.batch.item.UnexpectedInputException; import org.springframework.batch.item.util.ExecutionContextUserSupport; import org.springframework.core.io.Resource; @@ -96,19 +94,6 @@ public class MultiResourceItemReader implements ItemReader, ItemStream { return item; } - /** - * Remove the longer needed items from buffer, mark the index position and - * call mark() on delegate so that it clears its buffers. - */ - public void mark() throws MarkFailedException { - } - - /** - * Switches to 'read from buffer' state. - */ - public void reset() throws ResetFailedException { - } - /** * Close the {@link #setDelegate(ResourceAwareItemReaderItemStream)} reader * and reset instance variable values. diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemReader.java index 4f7d3e1b5..ca86d4ad7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemReader.java @@ -17,8 +17,6 @@ package org.springframework.batch.item.support; import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.MarkFailedException; -import org.springframework.batch.item.ResetFailedException; /** @@ -28,9 +26,4 @@ import org.springframework.batch.item.ResetFailedException; */ public abstract class AbstractItemReader implements ItemReader { - public void mark() throws MarkFailedException { - } - - public void reset() throws ResetFailedException { - } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemReaderItemStream.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemReaderItemStream.java index 87f025dc5..5a2ad976f 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemReaderItemStream.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AbstractItemReaderItemStream.java @@ -63,18 +63,6 @@ public abstract class AbstractItemReaderItemStream implements ItemReader, return doRead(); } - /** - * Mark is supported as long as this {@link ItemStream} is used in a - * single-threaded environment. The state backing the mark is a single - * counter, keeping track of the current position, so multiple threads - * cannot be accommodated. - */ - public void mark() { - } - - public void reset() { - } - protected int getCurrentItemCount() { return currentItemCount; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AggregateItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AggregateItemReader.java index b98bdf534..ef6331c6e 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AggregateItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/AggregateItemReader.java @@ -22,8 +22,6 @@ import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.MarkFailedException; -import org.springframework.batch.item.ResetFailedException; /** * An {@link ItemReader} that delivers a list as its item, storing up objects @@ -96,12 +94,6 @@ public class AggregateItemReader implements ItemReader> { return true; } - public void mark() throws MarkFailedException { - } - - public void reset() throws ResetFailedException { - } - public void setItemReader(ItemReader> itemReader) { this.itemReader = itemReader; } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractJdbcItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractJdbcItemReaderIntegrationTests.java index 8672b80c8..9038610fb 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractJdbcItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/AbstractJdbcItemReaderIntegrationTests.java @@ -1,7 +1,14 @@ package org.springframework.batch.item.database; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +import javax.sql.DataSource; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; @@ -9,14 +16,8 @@ import org.springframework.batch.item.sample.Foo; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.util.Assert; -import org.springframework.transaction.annotation.Transactional; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; -import org.junit.Before; -import org.junit.After; -import org.junit.Test; - -import javax.sql.DataSource; +import org.springframework.transaction.annotation.Transactional; /** * Common scenarios for testing {@link ItemReader} implementations which read data from database. diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooInputSource.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooInputSource.java index cf64d740d..55a0d0d0f 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooInputSource.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/FooInputSource.java @@ -57,17 +57,4 @@ class FooItemReader implements ItemStream, ItemReader, DisposableBean, Init itemReader.close(executionContext); } - /* - * (non-Javadoc) - * @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.StreamContext) - */ - public void mark() { - } - - /* - * (non-Javadoc) - * @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.StreamContext) - */ - public void reset() { - } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemReaderAdvancedTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemReaderAdvancedTests.java index 70f31173e..2938a687a 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemReaderAdvancedTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemReaderAdvancedTests.java @@ -136,7 +136,6 @@ public class FlatFileItemReaderAdvancedTests extends TestCase { assertEquals("[testLine1testLine2]", reader.read().toString()); assertEquals("[testLine3testLine4]", reader.read().toString()); - reader.mark(); reader.update(executionContext); reader.close(executionContext); @@ -156,15 +155,13 @@ public class FlatFileItemReaderAdvancedTests extends TestCase { // read some records reader.read(); reader.read(); - // commit them - reader.mark(); + // get restart data + reader.update(executionContext); // read next two records reader.read(); reader.read(); - // get restart data - reader.update(executionContext); - assertEquals(4, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class) + assertEquals(2, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class) + ".read.count")); // close input reader.close(executionContext); @@ -175,11 +172,11 @@ public class FlatFileItemReaderAdvancedTests extends TestCase { reader.open(executionContext); // read remaining records - assertEquals("[testLine5]", reader.read().toString()); - assertEquals("[testLine6]", reader.read().toString()); + assertEquals("[testLine3]", reader.read().toString()); + assertEquals("[testLine4]", reader.read().toString()); reader.update(executionContext); - assertEquals(6, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class) + assertEquals(4, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class) + ".read.count")); } @@ -193,15 +190,13 @@ public class FlatFileItemReaderAdvancedTests extends TestCase { // read some records reader.read(); reader.read(); - // commit them - reader.mark(); + // get restart data + reader.update(executionContext); // read next two records reader.read(); reader.read(); - // get restart data - reader.update(executionContext); - assertEquals(4, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class) + assertEquals(2, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class) + ".read.count")); // close input reader.close(executionContext); @@ -212,11 +207,11 @@ public class FlatFileItemReaderAdvancedTests extends TestCase { reader.open(executionContext); // read remaining records - assertEquals("[testLine5]", reader.read().toString()); - assertEquals("[testLine6]", reader.read().toString()); + assertEquals("[testLine3]", reader.read().toString()); + assertEquals("[testLine4]", reader.read().toString()); reader.update(executionContext); - assertEquals(6, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class) + assertEquals(4, executionContext.getLong(ClassUtils.getShortName(FlatFileItemReader.class) + ".read.count")); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/DelegatingItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/DelegatingItemReaderTests.java index 3e80c13dd..1ddf798dd 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/DelegatingItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/DelegatingItemReaderTests.java @@ -89,12 +89,6 @@ public class DelegatingItemReaderTests extends TestCase { public void open(ExecutionContext executionContext) { } - public void mark() { - } - - public void reset() { - } - } } diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/CustomItemReaderTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/CustomItemReaderTests.java index 45c7bbfbc..5eb484d78 100644 --- a/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/CustomItemReaderTests.java +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/common/CustomItemReaderTests.java @@ -18,18 +18,16 @@ package org.springframework.batch.sample.common; import java.util.ArrayList; import java.util.List; +import junit.framework.TestCase; + import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.ItemStreamException; -import org.springframework.batch.item.MarkFailedException; import org.springframework.batch.item.NoWorkFoundException; import org.springframework.batch.item.ParseException; -import org.springframework.batch.item.ResetFailedException; import org.springframework.batch.item.UnexpectedInputException; -import junit.framework.TestCase; - /** * Unit test class that was used as part of the Reference Documentation. I'm only including it in the * code to help keep the reference documentation up to date as the code base shifts. @@ -63,15 +61,6 @@ public class CustomItemReaderTests extends TestCase { assertNull(itemReader.read()); } - public void testRollback() throws Exception{ - - itemReader.mark(); - assertEquals("1", itemReader.read()); - assertEquals("2", itemReader.read()); - itemReader.reset(); - assertEquals("1", itemReader.read()); - } - public void testRestart() throws Exception{ ExecutionContext executionContext = new ExecutionContext(); @@ -88,11 +77,10 @@ public class CustomItemReaderTests extends TestCase { assertEquals("2", itemReader.read()); } - public class CustomItemReader implements ItemReader, ItemStream{ + public class CustomItemReader implements ItemReader, ItemStream { List items; int currentIndex = 0; - int lastMarkedIndex = 0; private static final String CURRENT_INDEX = "current.index"; public CustomItemReader(List items) { @@ -108,21 +96,12 @@ public class CustomItemReaderTests extends TestCase { return null; } - public void mark() throws MarkFailedException { - lastMarkedIndex = currentIndex; - }; - - public void reset() throws ResetFailedException { - currentIndex = lastMarkedIndex; - } - public void open(ExecutionContext executionContext) throws ItemStreamException { if(executionContext.containsKey(CURRENT_INDEX)){ currentIndex = new Long(executionContext.getLong(CURRENT_INDEX)).intValue(); } else{ currentIndex = 0; - lastMarkedIndex = 0; } }