public

(Spring-Batch) Example implementation wraps a MultiResourceItemReader to get the current resource.

  • Download Gist
WrappedMrirToGetCurrentResource.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.