Class RepositoryItemReader<T>
- All Implemented Interfaces:
ItemReader<T>,ItemStream,ItemStreamReader<T>,org.springframework.beans.factory.InitializingBean
A ItemReader that reads records utilizing a
PagingAndSortingRepository.
Performance of the reader is dependent on the repository implementation, however setting a reasonably large page size and matching that to the commit interval should yield better performance.
The reader must be configured with a
PagingAndSortingRepository, a
Sort, and a pageSize greater than 0.
This implementation is thread-safe between calls to AbstractItemCountingItemStreamItemReader.open(ExecutionContext),
but remember to use saveState=false if used in a multi-threaded client (no
restart available).
It is important to note that this is a paging item reader and exceptions that are
thrown while reading the page itself (mapping results to objects, etc in the
doPageRead()) will not be skippable since this reader has
no way of knowing if an exception should be skipped and therefore will continue to read
the same page until the skip limit is exceeded.
NOTE: The RepositoryItemReader only reads Java Objects i.e. non primitives.
- Since:
- 2.2
- Author:
- Michael Minella, Antoine Kapps
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected voiddoClose()Close the resources opened inAbstractItemCountingItemStreamItemReader.doOpen().protected voiddoOpen()Open resources necessary to start reading input.Performs the actual reading of a page via the repository.protected TdoRead()Read next item from input.protected voidjumpToItem(int itemLastIndex) Move to the given item index.voidsetArguments(List<?> arguments) Arguments to be passed to the data providing method.voidsetMethodName(String methodName) Specifies what method on the repository to call.voidsetPageSize(int pageSize) voidsetRepository(org.springframework.data.repository.PagingAndSortingRepository<?, ?> repository) ThePagingAndSortingRepositoryimplementation used to read input from.voidProvides ordering of the results so that order is maintained between paged queriesMethods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, updateMethods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, setExecutionContextName, setName
-
Field Details
-
logger
protected org.apache.commons.logging.Log logger
-
-
Constructor Details
-
RepositoryItemReader
public RepositoryItemReader()
-
-
Method Details
-
setArguments
Arguments to be passed to the data providing method.- Parameters:
arguments- list of method arguments to be passed to the repository
-
setSort
Provides ordering of the results so that order is maintained between paged queries- Parameters:
sorts- the fields to sort by and the directions
-
setPageSize
public void setPageSize(int pageSize) - Parameters:
pageSize- The number of items to retrieve per page.
-
setRepository
public void setRepository(org.springframework.data.repository.PagingAndSortingRepository<?, ?> repository) ThePagingAndSortingRepositoryimplementation used to read input from.- Parameters:
repository- underlying repository for input to be read from.
-
setMethodName
Specifies what method on the repository to call. This method must takePageableas the last argument.- Parameters:
methodName- name of the method to invoke
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
doRead
Description copied from class:AbstractItemCountingItemStreamItemReaderRead next item from input.- Specified by:
doReadin classAbstractItemCountingItemStreamItemReader<T>- Returns:
- an item or
nullif the data source is exhausted - Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
jumpToItem
Description copied from class:AbstractItemCountingItemStreamItemReaderMove to the given item index. Subclasses should override this method if there is a more efficient way of moving to given index than re-reading the input usingAbstractItemCountingItemStreamItemReader.doRead().- Overrides:
jumpToItemin classAbstractItemCountingItemStreamItemReader<T>- Parameters:
itemLastIndex- index of item (0 based) to jump to.- Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doPageRead
Performs the actual reading of a page via the repository. Available for overriding as needed.- Returns:
- the list of items that make up the page
- Throws:
Exception- Based on what the underlying method throws or related to the calling of the method
-
doOpen
Description copied from class:AbstractItemCountingItemStreamItemReaderOpen resources necessary to start reading input.- Specified by:
doOpenin classAbstractItemCountingItemStreamItemReader<T>- Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doClose
Description copied from class:AbstractItemCountingItemStreamItemReaderClose the resources opened inAbstractItemCountingItemStreamItemReader.doOpen().- Specified by:
doClosein classAbstractItemCountingItemStreamItemReader<T>- Throws:
Exception- Allows subclasses to throw checked exceptions for interpretation by the framework
-