org.springframework.batch.item.database.support
Class SingleColumnJdbcKeyCollector

java.lang.Object
  extended by org.springframework.batch.item.ExecutionContextUserSupport
      extended by org.springframework.batch.item.database.support.SingleColumnJdbcKeyCollector
All Implemented Interfaces:
KeyCollector

public class SingleColumnJdbcKeyCollector
extends ExecutionContextUserSupport
implements KeyCollector

Jdbc 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 RowMapper that returns a mapped key. By default, the 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.

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.

Author:
Lucas Ward
See Also:
SingleColumnRowMapper

Constructor Summary
SingleColumnJdbcKeyCollector()
           
SingleColumnJdbcKeyCollector(JdbcTemplate jdbcTemplate, String sql)
          Constructs a new instance using the provided jdbcTemplate and string representing the sql statement that should be used to retrieve keys.
 
Method Summary
 void afterPropertiesSet()
           
 List retrieveKeys(ExecutionContext executionContext)
          Retrieve the keys to be iterated over.
 void setJdbcTemplate(JdbcTemplate jdbcTemplate)
          Set the JdbcTemplate to be used.
 void setKeyMapper(RowMapper keyMapper)
          Set the RowMapper to be used to map each key to an object.
 void setRestartSql(String restartSql)
          Set the SQL query to be used to return the remaining keys to be processed.
 void setSql(String sql)
          Set the SQL statement to be used to return the keys to be processed.
 void updateContext(Object key, ExecutionContext executionContext)
          Get the restart data representing the last processed key.
 
Methods inherited from class org.springframework.batch.item.ExecutionContextUserSupport
getKey, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SingleColumnJdbcKeyCollector

public SingleColumnJdbcKeyCollector()

SingleColumnJdbcKeyCollector

public SingleColumnJdbcKeyCollector(JdbcTemplate jdbcTemplate,
                                    String sql)
Constructs a new instance using the provided jdbcTemplate and string representing the sql statement that should be used to retrieve keys.

Parameters:
jdbcTemplate -
sql -
Throws:
IllegalArgumentException - if jdbcTemplate is null.
IllegalArgumentException - if sql string is empty or null.
Method Detail

retrieveKeys

public List retrieveKeys(ExecutionContext executionContext)
Description copied from interface: KeyCollector

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.

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.

Specified by:
retrieveKeys in interface KeyCollector
Parameters:
executionContext - ExecutionContext containing any potential initial state that could potentially be used to retrieve the correct keys.
Returns:
list of keys returned by the driving query

updateContext

public void updateContext(Object key,
                          ExecutionContext executionContext)
Get the restart data representing the last processed key.

Specified by:
updateContext in interface KeyCollector
Parameters:
key - to be converted to restart data.
Throws:
IllegalArgumentException - if key is null.

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Throws:
Exception

setKeyMapper

public void setKeyMapper(RowMapper keyMapper)
Set the RowMapper to be used to map each key to an object.

Parameters:
keyMapper -

setRestartSql

public void setRestartSql(String restartSql)
Set the SQL query to be used to return the remaining keys to be processed.

Parameters:
restartSql -

setSql

public void setSql(String sql)
Set the SQL statement to be used to return the keys to be processed.

Parameters:
sql -

setJdbcTemplate

public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
Set the JdbcTemplate to be used.

Parameters:
jdbcTemplate -


Copyright © 2008 SpringSource. All Rights Reserved.