org.springframework.batch.item.database
Class DrivingQueryItemReader

java.lang.Object
  extended by org.springframework.batch.item.database.DrivingQueryItemReader
All Implemented Interfaces:
ItemReader, ItemStream, InitializingBean
Direct Known Subclasses:
IbatisDrivingQueryItemReader

public class DrivingQueryItemReader
extends Object
implements ItemReader, InitializingBean, ItemStream

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:

 CREATE TABLE CUSTOMER (
   ID BIGINT IDENTITY PRIMARY KEY,  
   NAME VARCHAR(45),
   CREDIT FLOAT
 );
 

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 ItemReader.read().

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.

The implementation is *not* thread-safe.

Author:
Lucas Ward

Constructor Summary
DrivingQueryItemReader()
           
DrivingQueryItemReader(List keys)
          Initialize the input source with the provided keys list.
 
Method Summary
 void afterPropertiesSet()
           
 void close(ExecutionContext executionContext)
          Close the resource by setting the list of keys to null, allowing them to be garbage collected.
protected  Object getCurrentKey()
          Get the current key.
 void mark()
          Mark is supported as long as this ItemStream is used in a single-threaded environment.
 void open(ExecutionContext executionContext)
          Initialize the item reader by delegating to the subclass in order to retrieve the keys.
 Object read()
          Return the next key in the List.
 void reset()
          Reset the stream to the last mark.
 void setKeyCollector(KeyCollector keyCollector)
          Set the key generation strategy to use for this input source.
 void setSaveState(boolean saveState)
           
 void update(ExecutionContext executionContext)
          Indicates that the execution context provided during open is about to be saved.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DrivingQueryItemReader

public DrivingQueryItemReader()

DrivingQueryItemReader

public DrivingQueryItemReader(List keys)
Initialize the input source with the provided keys list.

Parameters:
keys -
Method Detail

read

public Object read()
Return the next key in the List.

Specified by:
read in interface ItemReader
Returns:
next key in the list if not index is not at the last element, null otherwise.

getCurrentKey

protected Object getCurrentKey()
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.

Returns:
the current key.

close

public void close(ExecutionContext executionContext)
Close the resource by setting the list of keys to null, allowing them to be garbage collected.

Specified by:
close in interface ItemStream
Parameters:
executionContext - the current execution context in case it is needed

open

public void open(ExecutionContext executionContext)
Initialize the item reader by delegating to the subclass in order to retrieve the keys.

Specified by:
open in interface ItemStream
Throws:
IllegalStateException - if the keys list is null or initialized is true.

update

public void update(ExecutionContext executionContext)
Description copied from interface: ItemStream
Indicates that the execution context provided during open is about to be saved. If any state is remaining, but has not been put in the context, it should be added here.

Specified by:
update in interface ItemStream
Parameters:
executionContext - to be updated

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

setKeyCollector

public void setKeyCollector(KeyCollector keyCollector)
Set the key generation strategy to use for this input source.

Parameters:
keyCollector -

mark

public void mark()
Mark is supported as long as this ItemStream is used in a single-threaded environment. The state backing the mark is a single counter, keeping track of the current position, so multiple threads cannot be accommodated.

Specified by:
mark in interface ItemReader
See Also:
ItemReader.mark()

reset

public void reset()
Description copied from interface: ItemReader
Reset the stream to the last mark. After a reset the stream state will be such that changes (items read or written) since the last call to mark will not be visible after a call to close.

Specified by:
reset in interface ItemReader

setSaveState

public void setSaveState(boolean saveState)


Copyright © 2009 SpringSource. All Rights Reserved.