org.springframework.batch.item.file
Class MultiResourceItemReader

java.lang.Object
  extended by org.springframework.batch.item.util.ExecutionContextUserSupport
      extended by org.springframework.batch.item.file.MultiResourceItemReader
All Implemented Interfaces:
ItemReader, ItemStream

public class MultiResourceItemReader
extends ExecutionContextUserSupport
implements ItemReader, ItemStream

Reads items from multiple resources sequentially - resource list is given by setResources(Resource[]), the actual reading is delegated to setDelegate(ResourceAwareItemReaderItemStream). Input resources are ordered using setComparator(Comparator) to make sure resource ordering is preserved between job runs in restart scenario. Reset (rollback) capability is implemented by item buffering.

Author:
Robert Kasanicky

Constructor Summary
MultiResourceItemReader()
           
 
Method Summary
 void close(ExecutionContext executionContext)
          Close the setDelegate(ResourceAwareItemReaderItemStream) reader and reset instance variable values.
 void mark()
          Remove the longer needed items from buffer, mark the index position and call mark() on delegate so that it clears its buffers.
 void open(ExecutionContext executionContext)
          Figure out which resource to start with in case of restart, open the delegate and restore delegate's position in the resource.
 Object read()
          Reads the next item, jumping to next resource if necessary.
 void reset()
          Switches to 'read from buffer' state.
 void setComparator(Comparator comparator)
           
 void setDelegate(ResourceAwareItemReaderItemStream delegate)
           
 void setResources(Resource[] resources)
           
 void setSaveState(boolean saveState)
          Set the boolean indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream call to update.
 void update(ExecutionContext executionContext)
          Store the current resource index and position in the resource.
 
Methods inherited from class org.springframework.batch.item.util.ExecutionContextUserSupport
getKey, setName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiResourceItemReader

public MultiResourceItemReader()
Method Detail

read

public Object read()
            throws Exception,
                   UnexpectedInputException,
                   NoWorkFoundException,
                   ParseException
Reads the next item, jumping to next resource if necessary.

Specified by:
read in interface ItemReader
Throws:
Exception - if an underlying resource is unavailable.
UnexpectedInputException
NoWorkFoundException
ParseException

mark

public void mark()
          throws MarkFailedException
Remove the longer needed items from buffer, mark the index position and call mark() on delegate so that it clears its buffers.

Specified by:
mark in interface ItemReader
Throws:
MarkFailedException - if there is a problem with the mark. If a mark fails inside a transaction, it would be worrying, but not normally fatal.

reset

public void reset()
           throws ResetFailedException
Switches to 'read from buffer' state.

Specified by:
reset in interface ItemReader
Throws:
ResetFailedException - if there is a problem with the reset. If a reset fails inside a transaction, it would normally be fatal, and would leave the stream in an inconsistent state. So while this is an unchecked exception, it may be important for a client to catch it explicitly.
See Also:
ItemReader.reset()

close

public void close(ExecutionContext executionContext)
           throws ItemStreamException
Close the setDelegate(ResourceAwareItemReaderItemStream) reader and reset instance variable values.

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

open

public void open(ExecutionContext executionContext)
          throws ItemStreamException
Figure out which resource to start with in case of restart, open the delegate and restore delegate's position in the resource.

Specified by:
open in interface ItemStream
Throws:
ItemStreamException

update

public void update(ExecutionContext executionContext)
            throws ItemStreamException
Store the current resource index and position in the resource.

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

setDelegate

public void setDelegate(ResourceAwareItemReaderItemStream delegate)
Parameters:
delegate - reads items from single Resource.

setSaveState

public void setSaveState(boolean saveState)
Set the boolean indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream call to update.

Parameters:
saveState -

setComparator

public void setComparator(Comparator comparator)
Parameters:
comparator - used to order the injected resources, by default compares Resource.getFilename() values.

setResources

public void setResources(Resource[] resources)
Parameters:
resources - input resources


Copyright © 2009 SpringSource. All Rights Reserved.