Updates builders to take advantage of abstract base classes.

Specifically AbstractItemCountingItemStreamItemReaderBuilder or AbstractItemStreamSupportBuilder

resolves BATCH-2608
This commit is contained in:
Glenn Renfro
2017-05-15 16:19:33 -04:00
committed by Michael Minella
parent 8f05c3e11e
commit 4d9b13ce92
15 changed files with 178 additions and 553 deletions

View File

@@ -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<T> extends AbstractItemStreamSupportBuilder<T> {
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;
}
}

View File

@@ -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<T> {
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;
}
}

View File

@@ -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;
* </ul>
*
* @author Michael Minella
* @author Glenn Renfro
* @since 4.0
* @see HibernateCursorItemReader
*/
public class HibernateCursorItemReaderBuilder<T> {
public class HibernateCursorItemReaderBuilder<T>
extends AbstractItemCountingItemStreamItemReaderBuilder<HibernateCursorItemReaderBuilder<T>> {
private Map<String, Object> parameterValues;
@@ -55,32 +58,10 @@ public class HibernateCursorItemReaderBuilder<T> {
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<T> 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<T> {
* @return this instance for method chaining
* @see HibernateCursorItemReader#setUseStatelessSession(boolean)
*/
public HibernateCursorItemReaderBuilder<T> useSatelessSession(boolean useStatelessSession) {
public HibernateCursorItemReaderBuilder<T> 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<T> 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<T> 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<T> 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<T> {
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);

View File

@@ -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;
* </ul>
*
* @author Michael Minella
* @author Glenn Renfro
* @since 4.0
* @see HibernatePagingItemReader
*/
public class HibernatePagingItemReaderBuilder<T> {
private String name;
private int currentItem = 0;
private int maxItemCount = Integer.MAX_VALUE;
private boolean saveState = true;
public class HibernatePagingItemReaderBuilder<T>
extends AbstractItemCountingItemStreamItemReaderBuilder<HibernatePagingItemReaderBuilder<T>> {
private int pageSize = 10;
@@ -63,60 +58,6 @@ public class HibernatePagingItemReaderBuilder<T> {
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<T> 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<T> 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<T> 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<T> 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<T> {
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);

View File

@@ -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<T> {
public class JdbcCursorItemReaderBuilder<T>
extends AbstractItemCountingItemStreamItemReaderBuilder<JdbcCursorItemReaderBuilder<T>> {
private DataSource dataSource;
@@ -46,10 +49,6 @@ public class JdbcCursorItemReaderBuilder<T> {
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<T> {
private boolean useSharedExtendedConnection;
private boolean saveState = true;
private PreparedStatementSetter preparedStatementSetter;
private String sql;
private String name;
private RowMapper<T> rowMapper;
/**
@@ -120,49 +115,6 @@ public class JdbcCursorItemReaderBuilder<T> {
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<T> 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<T> 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<T> saveState(boolean saveState) {
this.saveState = saveState;
return this;
}
public JdbcCursorItemReaderBuilder<T> ignoreWarnings(boolean ignoreWarnings) {
this.ignoreWarnings = ignoreWarnings;
@@ -298,20 +250,6 @@ public class JdbcCursorItemReaderBuilder<T> {
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<T> name(String name) {
this.name = name;
return this;
}
/**
* Validates configuration and builds a new reader instance.
*

View File

@@ -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<T> {
public class JdbcPagingItemReaderBuilder<T>
extends AbstractItemCountingItemStreamItemReaderBuilder<JdbcPagingItemReaderBuilder<T>> {
private DataSource dataSource;
@@ -62,14 +65,6 @@ public class JdbcPagingItemReaderBuilder<T> {
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<T> {
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<T> 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<T> 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<T> 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<T> currentItemCount(int count) {
this.currentItemCount = count;
return this;
}
/**
* The SQL <code>GROUP BY</code> clause for a db specific @{@link PagingQueryProvider}.
* This is only used if a PaginingQueryProvider is not provided.

View File

@@ -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<T> {
private String name;
private int currentItem = 0;
private int maxItemCount = Integer.MAX_VALUE;
private boolean saveState = true;
public class JpaPagingItemReaderBuilder<T>
extends AbstractItemCountingItemStreamItemReaderBuilder<JpaPagingItemReaderBuilder<T>> {
private int pageSize = 10;
@@ -52,60 +47,6 @@ public class JpaPagingItemReaderBuilder<T> {
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<T> 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<T> 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<T> 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<T> 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<T> {
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);

View File

@@ -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<T> {
public class FlatFileItemReaderBuilder<T>
extends AbstractItemCountingItemStreamItemReaderBuilder<FlatFileItemReaderBuilder<T>> {
protected Log logger = LogFactory.getLog(getClass());
private String name;
private boolean strict = true;
private RecordSeparatorPolicy recordSeparatorPolicy =
@@ -68,8 +69,6 @@ public class FlatFileItemReaderBuilder<T> {
private Resource resource;
private int maxItemCount = Integer.MAX_VALUE;
private List<String> comments = new ArrayList<>();
private int linesToSkip = 0;
@@ -98,8 +97,6 @@ public class FlatFileItemReaderBuilder<T> {
private boolean beanMapperStrict = true;
private boolean saveState = true;
private BigInteger tokenizerValidator = new BigInteger("0");
/**
@@ -127,18 +124,6 @@ public class FlatFileItemReaderBuilder<T> {
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<T> maxItemCount(int maxItemCount) {
this.maxItemCount = maxItemCount;
return this;
}
/**
* Configure a custom {@link RecordSeparatorPolicy} for the reader.
*
@@ -349,33 +334,6 @@ public class FlatFileItemReaderBuilder<T> {
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<T> 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<T> name(String name) {
this.name = name;
return this;
}
/**
* Builds the {@link FlatFileItemReader}.
*
@@ -456,6 +414,7 @@ public class FlatFileItemReaderBuilder<T> {
reader.setSkippedLinesCallback(this.skippedLinesCallback);
reader.setRecordSeparatorPolicy(this.recordSeparatorPolicy);
reader.setMaxItemCount(this.maxItemCount);
reader.setCurrentItemCount(this.currentItemCount);
reader.setSaveState(this.saveState);
reader.setStrict(this.strict);

View File

@@ -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<T> {
public class FlatFileItemWriterBuilder<T> extends AbstractItemStreamSupportBuilder<FlatFileItemWriterBuilder<T>> {
private Resource resource;
@@ -47,31 +49,12 @@ public class FlatFileItemWriterBuilder<T> {
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<T> name(String name) {
this.name = name;
return this;
}
/**
* The {@link Resource} to be used as output.
*
@@ -181,20 +164,6 @@ public class FlatFileItemWriterBuilder<T> {
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<T> saveState(boolean saveState) {
this.saveState = saveState;
return this;
}
/**
* A callback for header processing.
*

View File

@@ -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<T> {
public class MultiResourceItemReaderBuilder<T>
extends AbstractItemStreamSupportBuilder<MultiResourceItemReaderBuilder<T>> {
private ResourceAwareItemReaderItemStream<? extends T> delegate;
private Resource[] resources;
private boolean saveState = true;
private boolean strict = false;
private String name;
private Comparator<Resource> comparator;
/**
@@ -76,22 +74,6 @@ public class MultiResourceItemReaderBuilder<T> {
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<T> 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<T> {
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<T> name(String name) {
this.name = name;
return this;
}
/**
* Used to order the injected resources, by default compares
* {@link Resource#getFilename()} values.

View File

@@ -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<T> {
public class MultiResourceItemWriterBuilder<T>
extends AbstractItemStreamSupportBuilder<MultiResourceItemWriterBuilder<T>> {
private Resource resource;
@@ -40,10 +43,6 @@ public class MultiResourceItemWriterBuilder<T> {
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<T> {
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<T> 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<T> name(String name) {
this.name = name;
return this;
}
/**
* Builds the {@link MultiResourceItemWriter}.
*

View File

@@ -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<T> {
public class StaxEventItemReaderBuilder<T>
extends AbstractItemCountingItemStreamItemReaderBuilder<StaxEventItemReaderBuilder<T>> {
private boolean strict = true;
@@ -41,29 +44,6 @@ public class StaxEventItemReaderBuilder<T> {
private List<String> 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<T> name(String name) {
this.name = name;
return this;
}
/**
* The {@link Resource} to be used as input.
*
@@ -91,9 +71,11 @@ public class StaxEventItemReaderBuilder<T> {
}
/**
* 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<T> {
}
/**
* 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<T> {
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<T> 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<T> 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<T> 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<T> {
StaxEventItemReader<T> 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);