BATCH-851: Removed DrivingQueryItemReader and related classes.
This commit is contained in:
@@ -1,177 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.database;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Convenience class for driving query item readers. Item readers of this type
|
||||
* use a 'driving query' to return back a list of keys. A key can be defined as
|
||||
* anything that can uniquely identify a record so that a more detailed record
|
||||
* can be retrieved for each object. This allows a much smaller footprint to be
|
||||
* stored in memory for processing. The following 'Customer' example table will
|
||||
* help illustrate this:
|
||||
*
|
||||
* <pre>
|
||||
* CREATE TABLE CUSTOMER (
|
||||
* ID BIGINT IDENTITY PRIMARY KEY,
|
||||
* NAME VARCHAR(45),
|
||||
* CREDIT FLOAT
|
||||
* );
|
||||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* A cursor based solution would simply open up a cursor over ID, NAME, and
|
||||
* CREDIT, and move it from one to the next. This can cause issues on databases
|
||||
* with pessimistic locking strategies. A 'driving query' approach would be to
|
||||
* return only the ID of the customer, then use a separate DAO to retrieve the
|
||||
* name and credit for each ID. This means that there will be a call to a
|
||||
* separate DAO for each call to {@link ItemReader#read()}.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Mutability: Because this base class cannot guarantee that the keys returned
|
||||
* by subclasses are immutable, care should be taken to not modify a key value.
|
||||
* Doing so would cause issues if a rollback occurs. For example, if a call to
|
||||
* read() is made, and the returned key is modified, a rollback will cause the
|
||||
* next call to read() to return the same object that was originally returned,
|
||||
* since there is no way to create a defensive copy, and re-querying the
|
||||
* database for all the keys would be too resource intensive.
|
||||
* </p>
|
||||
*
|
||||
* The implementation is *not* thread-safe.
|
||||
*
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @deprecated The DrivingQueryItemReader approach is not supported going forward, use a PagingItemReader
|
||||
* implementation instead. See {@link org.springframework.batch.item.database.AbstractPagingItemReader}
|
||||
*/
|
||||
public class DrivingQueryItemReader<T> implements ItemReader<T>, InitializingBean, ItemStream {
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
private T currentKey = null;
|
||||
|
||||
private Iterator<T> keysIterator;
|
||||
|
||||
private int currentIndex = 0;
|
||||
|
||||
private KeyCollector<T> keyCollector;
|
||||
|
||||
private boolean saveState = false;
|
||||
|
||||
public DrivingQueryItemReader() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the input source with the provided keys list.
|
||||
*
|
||||
* @param keys
|
||||
*/
|
||||
public DrivingQueryItemReader(List<T> keys) {
|
||||
this.keysIterator = keys.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the next key in the List.
|
||||
*
|
||||
* @return next key in the list if not index is not at the last element,
|
||||
* null otherwise.
|
||||
*/
|
||||
public T read() {
|
||||
|
||||
if (keysIterator.hasNext()) {
|
||||
currentIndex++;
|
||||
currentKey = keysIterator.next();
|
||||
return currentKey;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current key. This method will return the same object returned by
|
||||
* the last read() method. If no items have been read yet the ItemReader
|
||||
* yet, then null will be returned.
|
||||
*
|
||||
* @return the current key.
|
||||
*/
|
||||
protected T getCurrentKey() {
|
||||
return currentKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the resource by setting the list of keys to null, allowing them to
|
||||
* be garbage collected.
|
||||
*/
|
||||
public void close() {
|
||||
initialized = false;
|
||||
currentIndex = 0;
|
||||
keysIterator = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the item reader by delegating to the subclass in order to
|
||||
* retrieve the keys.
|
||||
*
|
||||
* @throws IllegalStateException if the keys list is null or initialized is
|
||||
* true.
|
||||
*/
|
||||
public void open(ExecutionContext executionContext) {
|
||||
|
||||
Assert.state(keysIterator == null && !initialized, "Cannot open an already opened item reader"
|
||||
+ ", call close() first.");
|
||||
List<T> keys = keyCollector.retrieveKeys(executionContext);
|
||||
Assert.notNull(keys, "Keys must not be null");
|
||||
keysIterator = keys.listIterator();
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
public void update(ExecutionContext executionContext) {
|
||||
if (saveState) {
|
||||
Assert.notNull(executionContext, "ExecutionContext must not be null");
|
||||
if (getCurrentKey() != null) {
|
||||
keyCollector.updateContext(getCurrentKey(), executionContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(keyCollector, "The KeyGenerator must not be null.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the key generation strategy to use for this input source.
|
||||
*
|
||||
* @param keyCollector
|
||||
*/
|
||||
public void setKeyCollector(KeyCollector<T> keyCollector) {
|
||||
this.keyCollector = keyCollector;
|
||||
}
|
||||
|
||||
public void setSaveState(boolean saveState) {
|
||||
this.saveState = saveState;
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.database;
|
||||
|
||||
import org.springframework.batch.item.database.support.IbatisKeyCollector;
|
||||
import org.springframework.orm.ibatis.SqlMapClientTemplate;
|
||||
|
||||
import com.ibatis.sqlmap.client.SqlMapClient;
|
||||
|
||||
/**
|
||||
* Extension of {@link DrivingQueryItemReader} that maps keys to objects. An
|
||||
* iBatis query id must be set to map and return each 'detail record'.
|
||||
*
|
||||
* The writer is thread safe after its properties are set (normal singleton
|
||||
* behaviour).
|
||||
*
|
||||
* TODO remove this class? It does not return keys so it shouldn't extend
|
||||
* DrivingQueryItemReader
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @see IbatisKeyCollector
|
||||
* @deprecated The DrivingQueryItemReader approach is not supported going forward, use a PagingItemReader
|
||||
* implementation instead. See {@link org.springframework.batch.item.database.AbstractPagingItemReader}
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public class IbatisDrivingQueryItemReader extends DrivingQueryItemReader {
|
||||
|
||||
private String detailsQueryId;
|
||||
|
||||
private SqlMapClientTemplate sqlMapClientTemplate;
|
||||
|
||||
/**
|
||||
* Overridden read() that uses the returned key as arguments to the details
|
||||
* query.
|
||||
*
|
||||
* @see org.springframework.batch.item.database.DrivingQueryItemReader#read()
|
||||
*/
|
||||
public Object read() {
|
||||
Object key = super.read();
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
return sqlMapClientTemplate.queryForObject(detailsQueryId, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param detailsQueryId id of the iBATIS select statement that will used to
|
||||
* retrieve an object for a single primary key from the list returned by
|
||||
* driving query
|
||||
*/
|
||||
public void setDetailsQueryId(String detailsQueryId) {
|
||||
this.detailsQueryId = detailsQueryId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link SqlMapClientTemplate} to use for this input source.
|
||||
*
|
||||
* @param sqlMapClient
|
||||
*/
|
||||
public void setSqlMapClient(SqlMapClient sqlMapClient) {
|
||||
this.sqlMapClientTemplate = new SqlMapClientTemplate(sqlMapClient);
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.database;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
|
||||
/**
|
||||
* Strategy interface used to collect keys in driving query input.
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @see DrivingQueryItemReader
|
||||
* @deprecated The DrivingQueryItemReader approach is not supported going forward, use a PagingItemReader
|
||||
* implementation instead. See {@link org.springframework.batch.item.database.AbstractPagingItemReader}
|
||||
*/
|
||||
public interface KeyCollector<T> {
|
||||
|
||||
/**
|
||||
* <p>Retrieve the keys to be iterated over. If the ExecutionContext
|
||||
* provided includes any state relevant to this collector (because it was
|
||||
* stored as part of saveState()) then it should start from the point
|
||||
* indicated by that state.</p>
|
||||
*
|
||||
* <p>In the case of a restart, (i.e. the ExecutionContext contains relevant state)
|
||||
* this method should return only the keys that are remaining to be processed. For
|
||||
* example, if the are 1,000 keys, and the 500th is processed before the batch job
|
||||
* terminates unexpectedly, upon restart keys 501 through 1,000 should be returned.
|
||||
* </p>
|
||||
*
|
||||
* @param executionContext ExecutionContext containing any potential initial state
|
||||
* that could potentially be used to retrieve the correct keys.
|
||||
* @return list of keys returned by the driving query (can be empty but not null)
|
||||
*/
|
||||
List<T> retrieveKeys(ExecutionContext executionContext);
|
||||
|
||||
/**
|
||||
* Given the provided key, store it in the provided ExecutionContext. This
|
||||
* is necessary because retrieveKeys() will be called with the ExecutionContext
|
||||
* that is provided as a result of this method. Since only the KeyCollector
|
||||
* can know what format the ExecutionContext should be in, it should also
|
||||
* save the state, as opposed to the {@link DrivingQueryItemReader} doing it
|
||||
* for all KeyCollector implementations.
|
||||
*
|
||||
* @param key to be converted to restart data.
|
||||
* @throws IllegalArgumentException if key is null.
|
||||
* @throws IllegalArgumentException if key is an incompatible type.
|
||||
*/
|
||||
void updateContext(T key, ExecutionContext executionContext);
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.database.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.database.DrivingQueryItemReader;
|
||||
import org.springframework.batch.item.database.KeyCollector;
|
||||
import org.springframework.batch.item.util.ExecutionContextUserSupport;
|
||||
import org.springframework.orm.ibatis.SqlMapClientTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import com.ibatis.sqlmap.client.SqlMapClient;
|
||||
|
||||
/**
|
||||
* {@link KeyCollector} based on iBATIS ORM framework. It is functionally
|
||||
* similar to {@link SingleColumnJdbcKeyCollector} but does not make assumptions
|
||||
* about the primary key structure. A separate restart query is necessary to
|
||||
* ensure that only the required keys remaining for processing are returned,
|
||||
* rather than the entire original list.</p>
|
||||
*
|
||||
* The writer is thread safe after its properties are set (normal singleton
|
||||
* behaviour).
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
* @author Lucas Ward
|
||||
* @see DrivingQueryItemReader
|
||||
* @deprecated The DrivingQueryItemReader approach is not supported going forward, use a PagingItemReader
|
||||
* implementation instead. See {@link org.springframework.batch.item.database.AbstractPagingItemReader}
|
||||
*/
|
||||
public class IbatisKeyCollector<T> extends ExecutionContextUserSupport implements KeyCollector<T> {
|
||||
|
||||
private static final String RESTART_KEY = "key.index";
|
||||
|
||||
private SqlMapClientTemplate sqlMapClientTemplate;
|
||||
|
||||
private String drivingQuery;
|
||||
|
||||
private String restartQueryId;
|
||||
|
||||
public IbatisKeyCollector() {
|
||||
setName(ClassUtils.getShortName(IbatisKeyCollector.class));
|
||||
}
|
||||
|
||||
/*
|
||||
* Retrieve the keys using the provided driving query id.
|
||||
*
|
||||
* @see KeyCollector#retrieveKeys()
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<T> retrieveKeys(ExecutionContext executionContext) {
|
||||
if (executionContext.containsKey(getKey(RESTART_KEY))) {
|
||||
Object key = executionContext.get(getKey(RESTART_KEY));
|
||||
return sqlMapClientTemplate.queryForList(restartQueryId, key);
|
||||
}
|
||||
else {
|
||||
return sqlMapClientTemplate.queryForList(drivingQuery);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see KeyCollector#saveState(Object, ExecutionContext)
|
||||
*/
|
||||
public void updateContext(T key, ExecutionContext executionContext) {
|
||||
Assert.notNull(key, "Key must not be null");
|
||||
Assert.notNull(executionContext, "ExecutionContext must be null");
|
||||
executionContext.put(getKey(RESTART_KEY), key);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(sqlMapClientTemplate, "SqlMaperClientTemplate must not be null.");
|
||||
Assert.hasText(drivingQuery, "The DrivingQuery must not be null or empty.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param sqlMapClient configured iBATIS client
|
||||
*/
|
||||
public void setSqlMapClient(SqlMapClient sqlMapClient) {
|
||||
this.sqlMapClientTemplate = new SqlMapClientTemplate();
|
||||
this.sqlMapClientTemplate.setSqlMapClient(sqlMapClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param drivingQueryId id of the iBATIS select statement that will be used
|
||||
* to retrieve the list of primary keys
|
||||
*/
|
||||
public void setDrivingQueryId(String drivingQueryId) {
|
||||
this.drivingQuery = drivingQueryId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the id of the restart query.
|
||||
*
|
||||
* @param restartQueryId id of the iBatis select statement that will be used
|
||||
* to retrieve the list of primary keys after a restart.
|
||||
*/
|
||||
public void setRestartQueryId(String restartQueryId) {
|
||||
this.restartQueryId = restartQueryId;
|
||||
}
|
||||
|
||||
public final SqlMapClientTemplate getSqlMapClientTemplate() {
|
||||
return sqlMapClientTemplate;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.database.support;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.database.DrivingQueryItemReader;
|
||||
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
|
||||
import org.springframework.batch.item.database.KeyCollector;
|
||||
import org.springframework.batch.item.util.ExecutionContextUserSupport;
|
||||
import org.springframework.jdbc.core.ColumnMapRowMapper;
|
||||
import org.springframework.jdbc.core.PreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* JDBC implementation of the {@link KeyCollector} interface that works for
|
||||
* composite keys. (i.e. keys represented by multiple columns) A SQL query to be
|
||||
* used to return the keys and a {@link ItemPreparedStatementSetter} to map each
|
||||
* row in the result set to an Object must be set in order to work correctly.
|
||||
* </p>
|
||||
*
|
||||
* The implementation is thread-safe as long as the
|
||||
* {@link #setPreparedStatementSetter(ItemPreparedStatementSetter)} and
|
||||
* {@link #setKeyMapper(RowMapper)} are thread-safe (true for default values).
|
||||
*
|
||||
*
|
||||
* @author Lucas Ward
|
||||
*
|
||||
* @see DrivingQueryItemReader
|
||||
* @see ItemPreparedStatementSetter
|
||||
*
|
||||
* TODO this class has nothing to do with "multiple columns" other than default
|
||||
* values form keyMapper and preparedStatementSetter. This should be sorted out for 2.0
|
||||
* @deprecated The DrivingQueryItemReader approach is not supported going forward, use a PagingItemReader
|
||||
* implementation instead. See {@link org.springframework.batch.item.database.AbstractPagingItemReader}
|
||||
*/
|
||||
public class MultipleColumnJdbcKeyCollector<T> extends ExecutionContextUserSupport implements KeyCollector<T> {
|
||||
|
||||
private static final String CURRENT_KEY = "current.key";
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
private RowMapper keyMapper = new ColumnMapRowMapper();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private ItemPreparedStatementSetter preparedStatementSetter = new ColumnMapItemPreparedStatementSetter();
|
||||
|
||||
private String sql;
|
||||
|
||||
private String restartSql;
|
||||
|
||||
public MultipleColumnJdbcKeyCollector() {
|
||||
setName(ClassUtils.getShortName(MultipleColumnJdbcKeyCollector.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new ItemReader.
|
||||
*
|
||||
* @param jdbcTemplate
|
||||
* @param sql - SQL statement that returns all keys to process. object.
|
||||
*/
|
||||
public MultipleColumnJdbcKeyCollector(JdbcOperations jdbcTemplate, String sql) {
|
||||
this();
|
||||
Assert.notNull(jdbcTemplate, "The JdbcTemplate must not be null.");
|
||||
Assert.hasText(sql, "The sql statement must not be null or empty.");
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.batch.io.sql.scratch.AbstractDrivingQueryItemReader
|
||||
* #retrieveKeys()
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<T> retrieveKeys(ExecutionContext executionContext) {
|
||||
|
||||
Assert.state(keyMapper != null, "KeyMapper must not be null.");
|
||||
Assert.state(StringUtils.hasText(restartSql), "The RestartQuery must not be null or empty"
|
||||
+ " in order to restart.");
|
||||
|
||||
if (executionContext.size() > 0) {
|
||||
T key = (T) executionContext.get(getKey(CURRENT_KEY));
|
||||
return jdbcTemplate.query(restartSql, new PreparedStatementSetterKeyWrapper(key, preparedStatementSetter),
|
||||
keyMapper);
|
||||
}
|
||||
else {
|
||||
return jdbcTemplate.query(sql, keyMapper);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.batch.io.driving.KeyGenerator#getKeyAsExecutionContext
|
||||
* (java.lang.Object)
|
||||
*/
|
||||
public void updateContext(T key, ExecutionContext executionContext) {
|
||||
Assert.notNull(key, "The key must not be null");
|
||||
Assert.notNull(executionContext, "The ExecutionContext must not be null");
|
||||
executionContext.put(getKey(CURRENT_KEY), key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the query to use to retrieve keys in order to restore the previous
|
||||
* state for restart.
|
||||
*
|
||||
* @param restartQuery
|
||||
*/
|
||||
public void setRestartSql(String restartQuery) {
|
||||
this.restartSql = restartQuery;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(jdbcTemplate, "The JdbcTemplate must not be null.");
|
||||
Assert.hasText(sql, "The DrivingQuery must not be null or empty.");
|
||||
Assert.notNull(keyMapper, "The key RowMapper must not be null.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link RowMapper} to be used to map a result set to keys.
|
||||
*
|
||||
* @param keyMapper
|
||||
*/
|
||||
public void setKeyMapper(RowMapper keyMapper) {
|
||||
this.keyMapper = keyMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sql statement used to generate the keys list.
|
||||
*
|
||||
* @param sql
|
||||
*/
|
||||
public void setSql(String sql) {
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
public void setPreparedStatementSetter(ItemPreparedStatementSetter<T> preparedStatementSetter) {
|
||||
this.preparedStatementSetter = preparedStatementSetter;
|
||||
}
|
||||
|
||||
private class PreparedStatementSetterKeyWrapper implements PreparedStatementSetter {
|
||||
|
||||
private T key;
|
||||
|
||||
private ItemPreparedStatementSetter<T> pss;
|
||||
|
||||
public PreparedStatementSetterKeyWrapper(T key, ItemPreparedStatementSetter<T> pss) {
|
||||
this.key = key;
|
||||
this.pss = pss;
|
||||
}
|
||||
|
||||
public void setValues(PreparedStatement ps) throws SQLException {
|
||||
pss.setValues(key, ps);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,170 +0,0 @@
|
||||
/*
|
||||
* Copyright 2006-2007 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.database.support;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.database.KeyCollector;
|
||||
import org.springframework.batch.item.util.ExecutionContextUserSupport;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.SingleColumnRowMapper;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Jdbc {@link KeyCollector} implementation that only works for a single column
|
||||
* key. A sql query must be passed in which will be used to return a list of
|
||||
* keys. Each key will be mapped by a {@link RowMapper} that returns a mapped
|
||||
* key. By default, the {@link SingleColumnRowMapper} is used, and will convert
|
||||
* keys into well known types at runtime. It is extremely important to note that
|
||||
* only one column should be mapped to an object and returned as a key. If
|
||||
* multiple columns are returned as a key in this strategy, then restart will
|
||||
* not function properly. Instead a strategy that supports keys comprised of
|
||||
* multiple columns should be used.
|
||||
* </p>
|
||||
*
|
||||
* <p>
|
||||
* Restartability: Because the key is only one column, restart is made much more
|
||||
* simple. Before each commit, the last processed key is returned to be stored
|
||||
* as restart data. Upon restart, that same key is given back to restore from,
|
||||
* using a separate 'RestartQuery'. This means that only the keys remaining to
|
||||
* be processed are returned, rather than returning the original list of keys
|
||||
* and iterating forward to that last committed point.
|
||||
* </p>
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @see SingleColumnRowMapper
|
||||
* @deprecated The DrivingQueryItemReader approach is not supported going forward, use a PagingItemReader
|
||||
* implementation instead. See {@link org.springframework.batch.item.database.AbstractPagingItemReader}
|
||||
*/
|
||||
public class SingleColumnJdbcKeyCollector<T> extends ExecutionContextUserSupport implements KeyCollector<T> {
|
||||
|
||||
private static final String RESTART_KEY = "key";
|
||||
|
||||
private JdbcOperations jdbcTemplate;
|
||||
|
||||
private String sql;
|
||||
|
||||
private String restartSql;
|
||||
|
||||
private RowMapper keyMapper = new SingleColumnRowMapper();
|
||||
|
||||
public SingleColumnJdbcKeyCollector() {
|
||||
setName(ClassUtils.getShortName(SingleColumnJdbcKeyCollector.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new instance using the provided jdbcTemplate and string
|
||||
* representing the sql statement that should be used to retrieve keys.
|
||||
*
|
||||
* @param jdbcTemplate
|
||||
* @param sql
|
||||
* @throws IllegalArgumentException if jdbcTemplate is null.
|
||||
* @throws IllegalArgumentException if sql string is empty or null.
|
||||
*/
|
||||
public SingleColumnJdbcKeyCollector(JdbcOperations jdbcTemplate, String sql) {
|
||||
this();
|
||||
Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null.");
|
||||
Assert.hasText(sql, "The sql statement must not be null or empty.");
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.batch.io.driving.KeyGenerationStrategy#retrieveKeys()
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<T> retrieveKeys(ExecutionContext executionContext) {
|
||||
|
||||
Assert.notNull(executionContext, "The ExecutionContext must not be null");
|
||||
|
||||
if (executionContext.containsKey(getKey(RESTART_KEY))) {
|
||||
Assert.state(StringUtils.hasText(restartSql), "The restart sql query must not be null or empty"
|
||||
+ " in order to restart.");
|
||||
return jdbcTemplate
|
||||
.query(restartSql, new Object[] { executionContext.get(getKey(RESTART_KEY)) }, keyMapper);
|
||||
}
|
||||
else {
|
||||
return jdbcTemplate.query(sql, keyMapper);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the restart data representing the last processed key.
|
||||
*
|
||||
* @throws IllegalArgumentException if key is null.
|
||||
*/
|
||||
public void updateContext(T key, ExecutionContext executionContext) {
|
||||
Assert.notNull(key, "The key must not be null.");
|
||||
Assert.notNull(executionContext, "The ExecutionContext must not be null");
|
||||
executionContext.put(getKey(RESTART_KEY), key);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(jdbcTemplate, "JdbcTemplate must not be null.");
|
||||
Assert.hasText(sql, "The DrivingQuery must not be null or empty.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link RowMapper} to be used to map each key to an object.
|
||||
*
|
||||
* @param keyMapper
|
||||
*/
|
||||
public void setKeyMapper(RowMapper keyMapper) {
|
||||
this.keyMapper = keyMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the SQL query to be used to return the remaining keys to be
|
||||
* processed.
|
||||
*
|
||||
* @param restartSql
|
||||
*/
|
||||
public void setRestartSql(String restartSql) {
|
||||
this.restartSql = restartSql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the SQL statement to be used to return the keys to be processed.
|
||||
*
|
||||
* @param sql
|
||||
*/
|
||||
public void setSql(String sql) {
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link JdbcOperations} to be used.
|
||||
*
|
||||
* @param jdbcTemplate
|
||||
*/
|
||||
public void setJdbcTemplate(JdbcOperations jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user