Skip to content

Instantly share code, notes, and snippets.

@langmi
Created January 9, 2012 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save langmi/1581781 to your computer and use it in GitHub Desktop.
Save langmi/1581781 to your computer and use it in GitHub Desktop.
(Spring-Batch) Example implementation wraps a MultiResourceItemReader to get the current resource.
public class WrappedMrirToGetCurrentResource<T> implements ItemStreamReader<T> {
private MultiResourceItemReader<T> mrir;
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
mrir.open(executionContext);
// current.resource is only available here if it's a restart
if (mrir.getCurrentResource() != null && mrir.getCurrentResource().getFilename() != null) {
System.out.println("open:" + mrir.getCurrentResource().getFilename());
}
}
@Override
public void update(ExecutionContext executionContext) throws ItemStreamException {
mrir.update(executionContext);
// one of the most reliable positions to get the current.resource
if (mrir.getCurrentResource() != null && mrir.getCurrentResource().getFilename() != null) {
System.out.println("update:" + mrir.getCurrentResource().getFilename());
}
}
@Override
public void close() throws ItemStreamException {
mrir.close();
}
@Override
public T read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
T result = mrir.read();
// another position to get the actual current.resource, has to be called
// after the first read
if (mrir.getCurrentResource() != null && mrir.getCurrentResource().getFilename() != null) {
System.out.println("read:" + mrir.getCurrentResource().getFilename());
}
return result;
}
public void setMrir(MultiResourceItemReader<T> mrir) {
this.mrir = mrir;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment