Created
January 9, 2012 07:57
-
-
Save langmi/1581781 to your computer and use it in GitHub Desktop.
(Spring-Batch) Example implementation wraps a MultiResourceItemReader to get the current resource.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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