diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/builder/AbstractItemCountingItemStreamItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/builder/AbstractItemCountingItemStreamItemReaderBuilder.java new file mode 100644 index 000000000..744e6efd8 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/builder/AbstractItemCountingItemStreamItemReaderBuilder.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.item.builder; + +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader; + +/** + * Abstract superclass for builders that create streams that support restart by storing + * item count in the {@link ExecutionContext} (therefore requires item ordering to be + * preserved between runs). + * + * @author Glenn Renfro + * + * @since 4.0 + */ +public abstract class AbstractItemCountingItemStreamItemReaderBuilder extends AbstractItemStreamSupportBuilder { + + protected int currentItemCount = 0; + + protected int maxItemCount = Integer.MAX_VALUE; + + /** + * Configure the max number of items to be read. + * + * @param maxItemCount the max items to be read + * @return The current instance of the builder. + * @see AbstractItemCountingItemStreamItemReader#setMaxItemCount(int) + */ + public T maxItemCount(int maxItemCount) { + this.maxItemCount = maxItemCount; + return (T) this; + } + + /** + * Index for the current item. Used on restarts to indicate where to start from. + * + * @param currentItemCount current index + * @return this instance for method chaining + * @see AbstractItemCountingItemStreamItemReader#setCurrentItemCount(int) + */ + public T currentItemCount(int currentItemCount) { + this.currentItemCount = currentItemCount; + + return (T) this; + } + +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/builder/AbstractItemStreamSupportBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/builder/AbstractItemStreamSupportBuilder.java new file mode 100644 index 000000000..a957aead6 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/builder/AbstractItemStreamSupportBuilder.java @@ -0,0 +1,57 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.batch.item.builder; + +import org.springframework.batch.item.ItemStreamSupport; + +/* + * Abstract superclass for builders that create streams that utilize {@link ItemStreamSupport}. + * @author Glenn Renfro + * + * @since 4.0 + */ +public class AbstractItemStreamSupportBuilder { + protected String name; + + protected boolean saveState = true; + + /** + * The name used to calculate the key within the + * {@link org.springframework.batch.item.ExecutionContext}. Required if + * {@link AbstractItemStreamSupportBuilder#saveState(boolean)} is set to true. + * + * @param name name of the reader instance + * @return The current instance of the builder. + * @see ItemStreamSupport#setName(String) + */ + public T name(String name) { + this.name = name; + return (T) this; + } + + /** + * Configure if the state of the {@link ItemStreamSupport} should be persisted within + * the {@link org.springframework.batch.item.ExecutionContext} for restart purposes. + * + * @param saveState defaults to true + * @return The current instance of the builder. + */ + public T saveState(boolean saveState) { + this.saveState = saveState; + return (T) this; + } +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernateCursorItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernateCursorItemReaderBuilder.java index 91f0f8600..e3f56d3fc 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernateCursorItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernateCursorItemReaderBuilder.java @@ -19,6 +19,7 @@ import java.util.Map; import org.hibernate.SessionFactory; +import org.springframework.batch.item.builder.AbstractItemCountingItemStreamItemReaderBuilder; import org.springframework.batch.item.database.HibernateCursorItemReader; import org.springframework.batch.item.database.orm.HibernateNativeQueryProvider; import org.springframework.batch.item.database.orm.HibernateQueryProvider; @@ -36,10 +37,12 @@ import org.springframework.util.StringUtils; * * * @author Michael Minella + * @author Glenn Renfro * @since 4.0 * @see HibernateCursorItemReader */ -public class HibernateCursorItemReaderBuilder { +public class HibernateCursorItemReaderBuilder + extends AbstractItemCountingItemStreamItemReaderBuilder> { private Map parameterValues; @@ -55,32 +58,10 @@ public class HibernateCursorItemReaderBuilder { private boolean useStatelessSession; - private int currentItem; - - private int maxItemCount = Integer.MAX_VALUE; - - private boolean saveState = true; - - private String name; - private String nativeQuery; private Class nativeClass; - /** - * A name used to prevent key collisions while saving the state in the - * {@link org.springframework.batch.item.ExecutionContext} - * - * @param name unique name for this reader instance - * @return this instance for method chaining - * @see HibernateCursorItemReader#setName(String) - */ - public HibernateCursorItemReaderBuilder name(String name) { - this.name = name; - - return this; - } - /** * A map of parameter values to be set on the query. The key of the map is the name * of the parameter to be set with the value being the value to be set. @@ -172,52 +153,12 @@ public class HibernateCursorItemReaderBuilder { * @return this instance for method chaining * @see HibernateCursorItemReader#setUseStatelessSession(boolean) */ - public HibernateCursorItemReaderBuilder useSatelessSession(boolean useStatelessSession) { + public HibernateCursorItemReaderBuilder useStatelessSession(boolean useStatelessSession) { this.useStatelessSession = useStatelessSession; return this; } - /** - * Index for the current item. Used on restarts to indicate where to start from. - * - * @param currentItem current index - * @return this instance for method chaining - * @see HibernateCursorItemReader#setCurrentItemCount(int) - */ - public HibernateCursorItemReaderBuilder currentItem(int currentItem) { - this.currentItem = currentItem; - - return this; - } - - /** - * The index of the max item to be read. - * - * @param maxItemCount max index - * @return this instance for method chaining - * @see HibernateCursorItemReader#setMaxItemCount(int) - */ - public HibernateCursorItemReaderBuilder maxItemCount(int maxItemCount) { - this.maxItemCount = maxItemCount; - - return this; - } - - /** - * Indicates if the state should be saved. If set to false, restarts will begin at - * the beginning of the dataset. Defaults to true - * - * @param saveState indicator - * @return this instance for method chaining - * @see HibernateCursorItemReader#setSaveState(boolean) - */ - public HibernateCursorItemReaderBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - /** * Used to configure a {@link HibernateNativeQueryProvider}. This is ignored if * @param nativeQuery @@ -284,7 +225,7 @@ public class HibernateCursorItemReaderBuilder { reader.setSessionFactory(this.sessionFactory); reader.setUseStatelessSession(this.useStatelessSession); - reader.setCurrentItemCount(this.currentItem); + reader.setCurrentItemCount(this.currentItemCount); reader.setMaxItemCount(this.maxItemCount); reader.setName(this.name); reader.setSaveState(this.saveState); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernatePagingItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernatePagingItemReaderBuilder.java index f610bb7db..745c187e7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernatePagingItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/HibernatePagingItemReaderBuilder.java @@ -19,6 +19,7 @@ import java.util.Map; import org.hibernate.SessionFactory; +import org.springframework.batch.item.builder.AbstractItemCountingItemStreamItemReaderBuilder; import org.springframework.batch.item.database.HibernatePagingItemReader; import org.springframework.batch.item.database.orm.HibernateQueryProvider; import org.springframework.util.Assert; @@ -34,18 +35,12 @@ import org.springframework.util.StringUtils; * * * @author Michael Minella + * @author Glenn Renfro * @since 4.0 * @see HibernatePagingItemReader */ -public class HibernatePagingItemReaderBuilder { - - private String name; - - private int currentItem = 0; - - private int maxItemCount = Integer.MAX_VALUE; - - private boolean saveState = true; +public class HibernatePagingItemReaderBuilder + extends AbstractItemCountingItemStreamItemReaderBuilder> { private int pageSize = 10; @@ -63,60 +58,6 @@ public class HibernatePagingItemReaderBuilder { private boolean statelessSession = true; - /** - * A name used to prevent key collisions while saving the state in the - * {@link org.springframework.batch.item.ExecutionContext} - * - * @param name unique name for this reader instance - * @return this instance for method chaining - * @see HibernatePagingItemReader#setName(String) - */ - public HibernatePagingItemReaderBuilder name(String name) { - this.name = name; - - return this; - } - - /** - * Index for the current item. Used on restarts to indicate where to start from. - * - * @param currentItem current index - * @return this instance for method chaining - * @see HibernatePagingItemReader#setCurrentItemCount(int) - */ - public HibernatePagingItemReaderBuilder currentItem(int currentItem) { - this.currentItem = currentItem; - - return this; - } - - /** - * The index of the max item to be read. - * - * @param maxItemCount max index - * @return this instance for method chaining - * @see HibernatePagingItemReader#setMaxItemCount(int) - */ - public HibernatePagingItemReaderBuilder maxItemCount(int maxItemCount) { - this.maxItemCount = maxItemCount; - - return this; - } - - /** - * Indicates if the state should be saved. If set to false, restarts will begin at - * the beginning of the dataset. Defaults to true - * - * @param saveState indicator - * @return this instance for method chaining - * @see HibernatePagingItemReader#setSaveState(boolean) - */ - public HibernatePagingItemReaderBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - /** * The number of records to request per page/query. Defaults to 10. Must be greater * than zero. @@ -252,7 +193,7 @@ public class HibernatePagingItemReaderBuilder { reader.setSessionFactory(this.sessionFactory); reader.setSaveState(this.saveState); reader.setMaxItemCount(this.maxItemCount); - reader.setCurrentItemCount(this.currentItem); + reader.setCurrentItemCount(this.currentItemCount); reader.setName(this.name); reader.setFetchSize(this.fetchSize); reader.setParameterValues(this.parameterValues); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java index 44d423b89..4baed5b6b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcCursorItemReaderBuilder.java @@ -19,6 +19,7 @@ import java.util.List; import javax.sql.DataSource; import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.builder.AbstractItemCountingItemStreamItemReaderBuilder; import org.springframework.batch.item.database.AbstractCursorItemReader; import org.springframework.batch.item.database.JdbcCursorItemReader; import org.springframework.batch.item.database.support.ListPreparedStatementSetter; @@ -34,9 +35,11 @@ import org.springframework.util.StringUtils; * Builder for the {@link JdbcCursorItemReader} * * @author Michael Minella + * @author Glenn Renfro * @since 4.0 */ -public class JdbcCursorItemReaderBuilder { +public class JdbcCursorItemReaderBuilder + extends AbstractItemCountingItemStreamItemReaderBuilder> { private DataSource dataSource; @@ -46,10 +49,6 @@ public class JdbcCursorItemReaderBuilder { private int queryTimeout = AbstractCursorItemReader.VALUE_NOT_SET; - private int currentItemCount = 0; - - private int maxItemCount = Integer.MAX_VALUE; - private boolean ignoreWarnings; private boolean verifyCursorPosition; @@ -58,14 +57,10 @@ public class JdbcCursorItemReaderBuilder { private boolean useSharedExtendedConnection; - private boolean saveState = true; - private PreparedStatementSetter preparedStatementSetter; private String sql; - private String name; - private RowMapper rowMapper; /** @@ -120,49 +115,6 @@ public class JdbcCursorItemReaderBuilder { return this; } - /** - * The index of the first record to begin reading from. Overridden if a previous value - * is provided via the {@link org.springframework.batch.item.ExecutionContext} on - * {@link org.springframework.batch.item.ItemStream#open(ExecutionContext)} - * - * @param currentItemCount current index - * @return this instance for method chaining - * @see JdbcCursorItemReader#setCurrentItemCount(int) - */ - public JdbcCursorItemReaderBuilder currentItemCount(int currentItemCount) { - this.currentItemCount = currentItemCount; - - return this; - } - - /** - * The max number of items to be read. Overriden if a previous value is povided via - * the {@link ExecutionContext} on {@link org.springframework.batch.item.ItemStream#open} - * - * @param maxItemCount count - * @return this instance for method chaining - * @see JdbcCursorItemReader#setMaxItemCount(int) - */ - public JdbcCursorItemReaderBuilder maxItemCount(int maxItemCount) { - this.maxItemCount = maxItemCount; - - return this; - } - - /** - * Indicates if the state of the reader should be persisted in the - * {@link ExecutionContext}. Defaults to true. - * - * @param saveState indicator. Defaults to true - * @return this instance for method chaining - * @see JdbcCursorItemReader#setSaveState(boolean) - */ - public JdbcCursorItemReaderBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - public JdbcCursorItemReaderBuilder ignoreWarnings(boolean ignoreWarnings) { this.ignoreWarnings = ignoreWarnings; @@ -298,20 +250,6 @@ public class JdbcCursorItemReaderBuilder { return this; } - /** - * A name used to prevent key collisions while saving state in the - * {@link ExecutionContext}. - * - * @param name unique name for this reader instance - * @return this instance for method chaining - * @see JdbcCursorItemReader#setName(String) - */ - public JdbcCursorItemReaderBuilder name(String name) { - this.name = name; - - return this; - } - /** * Validates configuration and builds a new reader instance. * diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java index 4830ccc49..f669e654e 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JdbcPagingItemReaderBuilder.java @@ -18,6 +18,7 @@ package org.springframework.batch.item.database.builder; import java.util.Map; import javax.sql.DataSource; +import org.springframework.batch.item.builder.AbstractItemCountingItemStreamItemReaderBuilder; import org.springframework.batch.item.database.JdbcPagingItemReader; import org.springframework.batch.item.database.Order; import org.springframework.batch.item.database.PagingQueryProvider; @@ -45,10 +46,12 @@ import org.springframework.util.Assert; * will be used. * * @author Michael Minella + * @author Glenn Renfro * @since 4.0 * @see JdbcPagingItemReader */ -public class JdbcPagingItemReaderBuilder { +public class JdbcPagingItemReaderBuilder + extends AbstractItemCountingItemStreamItemReaderBuilder> { private DataSource dataSource; @@ -62,14 +65,6 @@ public class JdbcPagingItemReaderBuilder { private int pageSize = 10; - private boolean saveState = true; - - private String name; - - private int maxItemCount = Integer.MAX_VALUE; - - private int currentItemCount = 0; - private String groupClause; private String selectClause; @@ -146,60 +141,6 @@ public class JdbcPagingItemReaderBuilder { return this; } - /** - * Set to false in a multithreaded environment (restart is disabled). If set to true, - * a name is required. Defaults to true. - * - * @param saveState determine if the reader's state should be persisted - * @return this instance for method chaining - * @see JdbcPagingItemReader#setSaveState(boolean) - */ - public JdbcPagingItemReaderBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - - /** - * A name used to prevent key collissions while saving the state in the - * {@link org.springframework.batch.item.ExecutionContext} - * - * @param name unique name for this reader instance - * @return this instance for method chaining - * @see JdbcPagingItemReader#setName(String) - */ - public JdbcPagingItemReaderBuilder name(String name) { - this.name = name; - - return this; - } - - /** - * Maximum number of items to read. - * - * @param count number of items - * @return this instance for method chaining - * @see JdbcPagingItemReader#setMaxItemCount(int) - */ - public JdbcPagingItemReaderBuilder maxItemCount(int count) { - this.maxItemCount = count; - - return this; - } - - /** - * The current index of the item to read. - * - * @param count current index - * @return this instance for method chaining - * @see JdbcPagingItemReader#setCurrentItemCount(int) - */ - public JdbcPagingItemReaderBuilder currentItemCount(int count) { - this.currentItemCount = count; - - return this; - } - /** * The SQL GROUP BY clause for a db specific @{@link PagingQueryProvider}. * This is only used if a PaginingQueryProvider is not provided. diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilder.java index 0d0efb1be..e3dba4904 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilder.java @@ -18,6 +18,7 @@ package org.springframework.batch.item.database.builder; import java.util.Map; import javax.persistence.EntityManagerFactory; +import org.springframework.batch.item.builder.AbstractItemCountingItemStreamItemReaderBuilder; import org.springframework.batch.item.database.JpaPagingItemReader; import org.springframework.batch.item.database.orm.JpaQueryProvider; import org.springframework.util.Assert; @@ -26,19 +27,13 @@ import org.springframework.util.Assert; * Creates a fully qualified JpaPagingItemReader. * * @author Michael Minella + * @author Glenn Renfro * * @since 4.0 */ -public class JpaPagingItemReaderBuilder { - - private String name; - - private int currentItem = 0; - - private int maxItemCount = Integer.MAX_VALUE; - - private boolean saveState = true; +public class JpaPagingItemReaderBuilder + extends AbstractItemCountingItemStreamItemReaderBuilder> { private int pageSize = 10; @@ -52,60 +47,6 @@ public class JpaPagingItemReaderBuilder { private JpaQueryProvider queryProvider; - /** - * A name used to prevent key collisions while saving the state in the - * {@link org.springframework.batch.item.ExecutionContext} - * - * @param name unique name for this reader instance - * @return this instance for method chaining - * @see JpaPagingItemReader#setName(String) - */ - public JpaPagingItemReaderBuilder name(String name) { - this.name = name; - - return this; - } - - /** - * Index for the current item. Used on restarts to indicate where to start from. - * - * @param currentItem current index - * @return this instance for method chaining - * @see JpaPagingItemReader#setCurrentItemCount(int) - */ - public JpaPagingItemReaderBuilder currentItem(int currentItem) { - this.currentItem = currentItem; - - return this; - } - - /** - * The index of the max item to be read. - * - * @param maxItemCount max index - * @return this instance for method chaining - * @see JpaPagingItemReader#setMaxItemCount(int) - */ - public JpaPagingItemReaderBuilder maxItemCount(int maxItemCount) { - this.maxItemCount = maxItemCount; - - return this; - } - - /** - * Indicates if the state should be saved. If set to false, restarts will begin at - * the beginning of the dataset. Defaults to true - * - * @param saveState indicator - * @return this instance for method chaining - * @see JpaPagingItemReader#setSaveState(boolean) - */ - public JpaPagingItemReaderBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - /** * The number of records to request per page/query. Defaults to 10. Must be greater * than zero. @@ -216,7 +157,7 @@ public class JpaPagingItemReaderBuilder { reader.setEntityManagerFactory(this.entityManagerFactory); reader.setQueryProvider(this.queryProvider); reader.setTransacted(this.transacted); - reader.setCurrentItemCount(this.currentItem); + reader.setCurrentItemCount(this.currentItemCount); reader.setMaxItemCount(this.maxItemCount); reader.setSaveState(this.saveState); reader.setName(this.name); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java index a7d113afc..46dfd1a80 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java @@ -29,6 +29,7 @@ import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.batch.item.builder.AbstractItemCountingItemStreamItemReaderBuilder; import org.springframework.batch.item.file.FlatFileItemReader; import org.springframework.batch.item.file.LineCallbackHandler; import org.springframework.batch.item.file.LineMapper; @@ -52,15 +53,15 @@ import org.springframework.util.StringUtils; * A builder implementation for the {@link FlatFileItemReader}. * * @author Michael Minella + * @author Glenn Renfro * @since 4.0 * @see FlatFileItemReader */ -public class FlatFileItemReaderBuilder { +public class FlatFileItemReaderBuilder + extends AbstractItemCountingItemStreamItemReaderBuilder> { protected Log logger = LogFactory.getLog(getClass()); - private String name; - private boolean strict = true; private RecordSeparatorPolicy recordSeparatorPolicy = @@ -68,8 +69,6 @@ public class FlatFileItemReaderBuilder { private Resource resource; - private int maxItemCount = Integer.MAX_VALUE; - private List comments = new ArrayList<>(); private int linesToSkip = 0; @@ -98,8 +97,6 @@ public class FlatFileItemReaderBuilder { private boolean beanMapperStrict = true; - private boolean saveState = true; - private BigInteger tokenizerValidator = new BigInteger("0"); /** @@ -127,18 +124,6 @@ public class FlatFileItemReaderBuilder { return this; } - /** - * Configure the max number of items to be read. - * - * @param maxItemCount the max items to be read - * @return The current instance of the builder. - * @see FlatFileItemReader#setMaxItemCount(int) - */ - public FlatFileItemReaderBuilder maxItemCount(int maxItemCount) { - this.maxItemCount = maxItemCount; - return this; - } - /** * Configure a custom {@link RecordSeparatorPolicy} for the reader. * @@ -349,33 +334,6 @@ public class FlatFileItemReaderBuilder { return this; } - /** - * Configure if the state of the {@link FlatFileItemReader} should be persisted within - * the {@link org.springframework.batch.item.ExecutionContext} for restart purposes. - * - * @param saveState defaults to true - * @return The current instance of the builder. - * @see FlatFileItemReader#setSaveState(boolean) - */ - public FlatFileItemReaderBuilder saveState(boolean saveState) { - this.saveState = saveState; - return this; - } - - /** - * The name used to calculate the key within the - * {@link org.springframework.batch.item.ExecutionContext}. Required if - * {@link FlatFileItemReaderBuilder#saveState(boolean)} is set to true. - * - * @param name name of the reader instance - * @return The current instance of the builder. - * @see FlatFileItemReader#setName(String) - */ - public FlatFileItemReaderBuilder name(String name) { - this.name = name; - return this; - } - /** * Builds the {@link FlatFileItemReader}. * @@ -456,6 +414,7 @@ public class FlatFileItemReaderBuilder { reader.setSkippedLinesCallback(this.skippedLinesCallback); reader.setRecordSeparatorPolicy(this.recordSeparatorPolicy); reader.setMaxItemCount(this.maxItemCount); + reader.setCurrentItemCount(this.currentItemCount); reader.setSaveState(this.saveState); reader.setStrict(this.strict); diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilder.java index 389822ea4..538677f19 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemWriterBuilder.java @@ -15,6 +15,7 @@ */ package org.springframework.batch.item.file.builder; +import org.springframework.batch.item.builder.AbstractItemStreamSupportBuilder; import org.springframework.batch.item.file.FlatFileFooterCallback; import org.springframework.batch.item.file.FlatFileHeaderCallback; import org.springframework.batch.item.file.FlatFileItemWriter; @@ -26,10 +27,11 @@ import org.springframework.util.Assert; * A builder implementation for the {@link FlatFileItemWriter} * * @author Michael Minella + * @author Glenn Renfro * @since 4.0 * @see FlatFileItemWriter */ -public class FlatFileItemWriterBuilder { +public class FlatFileItemWriterBuilder extends AbstractItemStreamSupportBuilder> { private Resource resource; @@ -47,31 +49,12 @@ public class FlatFileItemWriterBuilder { private boolean shouldDeleteIfEmpty = false; - private boolean saveState = true; - private FlatFileHeaderCallback headerCallback; private FlatFileFooterCallback footerCallback; private boolean transactional = FlatFileItemWriter.DEFAULT_TRANSACTIONAL; - private String name; - - /** - * The name used to calculate the key within the - * {@link org.springframework.batch.item.ExecutionContext}. Required if - * {@link FlatFileItemWriterBuilder#saveState(boolean)} is set to true. - * - * @param name name of the writer instance - * @return The current instance of the builder. - * @see FlatFileItemWriter#setName(String) - */ - public FlatFileItemWriterBuilder name(String name) { - this.name = name; - - return this; - } - /** * The {@link Resource} to be used as output. * @@ -181,20 +164,6 @@ public class FlatFileItemWriterBuilder { return this; } - /** - * If set to false, the state of the output is not maintained and restart is not - * supported. - * - * @param saveState defaults to true - * @return The current instance of the builder - * @see FlatFileItemWriter#setSaveState(boolean) - */ - public FlatFileItemWriterBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - /** * A callback for header processing. * diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/MultiResourceItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/MultiResourceItemReaderBuilder.java index 40c629e0d..4cc9725f7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/MultiResourceItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/MultiResourceItemReaderBuilder.java @@ -20,6 +20,7 @@ import java.util.Comparator; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemStream; +import org.springframework.batch.item.builder.AbstractItemStreamSupportBuilder; import org.springframework.batch.item.file.MultiResourceItemReader; import org.springframework.batch.item.file.ResourceAwareItemReaderItemStream; import org.springframework.core.io.Resource; @@ -33,18 +34,15 @@ import org.springframework.util.StringUtils; * @since 4.0 * @see MultiResourceItemReader */ -public class MultiResourceItemReaderBuilder { +public class MultiResourceItemReaderBuilder + extends AbstractItemStreamSupportBuilder> { private ResourceAwareItemReaderItemStream delegate; private Resource[] resources; - private boolean saveState = true; - private boolean strict = false; - private String name; - private Comparator comparator; /** @@ -76,22 +74,6 @@ public class MultiResourceItemReaderBuilder { return this; } - /** - * Set the boolean indicating whether or not state should be saved in the provided - * {@link ExecutionContext} during the {@link ItemStream} call to update. - * - * @param saveState true to update ExecutionContext. False do not update - * ExecutionContext. - * @return this instance for method chaining. - * @see MultiResourceItemReader#setSaveState(boolean) - * - */ - public MultiResourceItemReaderBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - /** * In strict mode the reader will throw an exception on * {@link MultiResourceItemReader#open(org.springframework.batch.item.ExecutionContext)} @@ -107,21 +89,6 @@ public class MultiResourceItemReaderBuilder { return this; } - /** - * The name of the component which will be used as a stem for keys in the - * {@link ExecutionContext}. Subclasses should provide a default value, e.g. the short - * form of the class name. - * - * @param name the name for the component. - * @return this instance for method chaining. - * @see MultiResourceItemReader#setName(String) - */ - public MultiResourceItemReaderBuilder name(String name) { - this.name = name; - - return this; - } - /** * Used to order the injected resources, by default compares * {@link Resource#getFilename()} values. diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/MultiResourceItemWriterBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/MultiResourceItemWriterBuilder.java index bbcda08ac..3beb634b7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/MultiResourceItemWriterBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/MultiResourceItemWriterBuilder.java @@ -17,6 +17,7 @@ package org.springframework.batch.item.file.builder; import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.item.builder.AbstractItemStreamSupportBuilder; import org.springframework.batch.item.file.MultiResourceItemWriter; import org.springframework.batch.item.file.ResourceAwareItemWriterItemStream; import org.springframework.batch.item.file.ResourceSuffixCreator; @@ -27,10 +28,12 @@ import org.springframework.util.Assert; * A builder implementation for the {@link MultiResourceItemWriter}. * * @author Glenn Renfro + * @author Glenn Renfro * @since 4.0 * @see MultiResourceItemWriter */ -public class MultiResourceItemWriterBuilder { +public class MultiResourceItemWriterBuilder + extends AbstractItemStreamSupportBuilder> { private Resource resource; @@ -40,10 +43,6 @@ public class MultiResourceItemWriterBuilder { private ResourceSuffixCreator suffixCreator; - private boolean saveState = true; - - private String name; - /** * Allows customization of the suffix of the created resources based on the index. * @@ -98,36 +97,6 @@ public class MultiResourceItemWriterBuilder { return this; } - /** - * Set the boolean indicating whether or not state should be saved in the provided - * {@link ExecutionContext} during the delegate call to update. - * - * @param saveState true to update ExecutionContext. False do not update - * ExecutionContext. - * @return The current instance of the builder. - * @see MultiResourceItemWriter#setSaveState(boolean) - */ - public MultiResourceItemWriterBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - - /** - * The name of the component which will be used as a stem for keys in the - * {@link ExecutionContext}. Subclasses should provide a default value, e.g. - * the short form of the class name. - * - * @param name the name for the component. - * @return The current instance of the builder. - * @see MultiResourceItemWriter#setName(String) - */ - public MultiResourceItemWriterBuilder name(String name) { - this.name = name; - - return this; - } - /** * Builds the {@link MultiResourceItemWriter}. * diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java index e795c5897..d6ddb251b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.springframework.batch.item.builder.AbstractItemCountingItemStreamItemReaderBuilder; import org.springframework.batch.item.xml.StaxEventItemReader; import org.springframework.core.io.Resource; import org.springframework.oxm.Unmarshaller; @@ -29,9 +30,11 @@ import org.springframework.util.StringUtils; * A fluent builder for the {@link StaxEventItemReader} * * @author Michael Minella + * @author Glenn Renfro * @since 4.0 */ -public class StaxEventItemReaderBuilder { +public class StaxEventItemReaderBuilder + extends AbstractItemCountingItemStreamItemReaderBuilder> { private boolean strict = true; @@ -41,29 +44,6 @@ public class StaxEventItemReaderBuilder { private List fragmentRootElements = new ArrayList<>(); - private int currentItemCount = 0; - - private int maxItemCount = Integer.MAX_VALUE; - - private boolean saveState = true; - - private String name; - - /** - * The name used to calculate the key within the - * {@link org.springframework.batch.item.ExecutionContext}. Required if - * {@link StaxEventItemReaderBuilder#saveState(boolean)} is set to true. - * - * @param name name of the reader instance - * @return The current instance of the builder. - * @see StaxEventItemReader#setName(String) - */ - public StaxEventItemReaderBuilder name(String name) { - this.name = name; - - return this; - } - /** * The {@link Resource} to be used as input. * @@ -91,9 +71,11 @@ public class StaxEventItemReaderBuilder { } /** - * Adds the list of fragments to be used as the root of each chunk to the configuration. + * Adds the list of fragments to be used as the root of each chunk to the + * configuration. * - * @param fragmentRootElements the XML root elements to be used to identify XML chunks. + * @param fragmentRootElements the XML root elements to be used to identify XML + * chunks. * @return The current instance of the builder. * @see StaxEventItemReader#setFragmentRootElementNames(String[]) */ @@ -104,9 +86,11 @@ public class StaxEventItemReaderBuilder { } /** - * Adds the list of fragments to be used as the root of each chunk to the configuration. + * Adds the list of fragments to be used as the root of each chunk to the + * configuration. * - * @param fragmentRootElements the XML root elements to be used to identify XML chunks. + * @param fragmentRootElements the XML root elements to be used to identify XML + * chunks. * @return The current instance of the builder. * @see StaxEventItemReader#setFragmentRootElementNames(String[]) */ @@ -116,51 +100,9 @@ public class StaxEventItemReaderBuilder { return this; } - /** - * The starting point for reading (offset number of items). This value is overriden - * on restart if saveState is set to true. - * - * @param currentItemCount item number to begin at - * @return The current instance of the builder. - * @see StaxEventItemReader#setCurrentItemCount(int) - */ - public StaxEventItemReaderBuilder currentItemCount(int currentItemCount) { - this.currentItemCount = currentItemCount; - - return this; - } - - /** - * The maximum number of items to read. - * - * @param maxItemCount max number of items to be read - * @return The current instance of the builder. - * @see StaxEventItemReader#setMaxItemCount(int) - */ - public StaxEventItemReaderBuilder maxItemCount(int maxItemCount) { - this.maxItemCount = maxItemCount; - - return this; - } - - /** - * Indicates that the state of the reader should be saved in the - * {@link org.springframework.batch.item.ExecutionContext} for restart. True by - * default. - * - * @param saveState indicates the state of the reader should be saved - * @return The current instance of the builder. - * @see StaxEventItemReader#setSaveState(boolean) - */ - public StaxEventItemReaderBuilder saveState(boolean saveState) { - this.saveState = saveState; - - return this; - } - /** * Setting this value to true indicates that it is an error if the input does not - * exist and an exception will be thrown. Defaults to true. + * exist and an exception will be thrown. Defaults to true. * * @param strict indicates the input file must exist * @return The current instance of the builder @@ -182,16 +124,14 @@ public class StaxEventItemReaderBuilder { StaxEventItemReader reader = new StaxEventItemReader<>(); - if(this.saveState) { - Assert.state(StringUtils.hasText(this.name), - "A name is required when saveState is set to true."); + if (this.saveState) { + Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); } else { reader.setName(this.name); } - Assert.notEmpty(this.fragmentRootElements, - "At least one fragment root element is required"); + Assert.notEmpty(this.fragmentRootElements, "At least one fragment root element is required"); reader.setSaveState(this.saveState); reader.setResource(this.resource); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/HibernateCursorItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/HibernateCursorItemReaderBuilderTests.java index ffa812fb2..2092c3b5e 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/HibernateCursorItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/HibernateCursorItemReaderBuilderTests.java @@ -71,10 +71,10 @@ public class HibernateCursorItemReaderBuilderTests { .name("fooReader") .sessionFactory(this.sessionFactory) .fetchSize(2) - .currentItem(2) + .currentItemCount(2) .maxItemCount(4) .queryName("allFoos") - .useSatelessSession(true) + .useStatelessSession(true) .build(); reader.afterPropertiesSet(); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/HibernatePagingItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/HibernatePagingItemReaderBuilderTests.java index dc48cab96..15f14eb2b 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/HibernatePagingItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/HibernatePagingItemReaderBuilderTests.java @@ -73,7 +73,7 @@ public class HibernatePagingItemReaderBuilderTests { .name("fooReader") .sessionFactory(this.sessionFactory) .fetchSize(2) - .currentItem(2) + .currentItemCount(2) .maxItemCount(4) .pageSize(5) .queryName("allFoos") diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilderTests.java index fe7436a87..824761554 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/database/builder/JpaPagingItemReaderBuilderTests.java @@ -73,7 +73,7 @@ public class JpaPagingItemReaderBuilderTests { JpaPagingItemReader reader = new JpaPagingItemReaderBuilder() .name("fooReader") .entityManagerFactory(this.entityManagerFactory) - .currentItem(2) + .currentItemCount(2) .maxItemCount(4) .pageSize(5) .transacted(false)