I'm already doing that for itemExtractParamsReader in the open() and update() methods at the bottom of the original code snippet. Since your suggestion I have now included the opening of the itemExtractFilterReader.open.
However the only way I can get it to run without any exception's being thrown is to set the queuestring of the itemExtractFilterReader to a temporary value in the beforeStep method and then in the read method reset the querystring with some additional criteria based on returned value from the first reader, but it retains the original/temporary querystring. To further illustrate this i have included the updated class.
Code:
package org.atoc.batch.dss.itemreader;
import org.atoc.batch.dss.vo.ExtractFiltersVO;
import org.atoc.batch.dss.vo.ExtractParamsVO;
import org.hibernate.SessionFactory;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.BeforeStep;
import org.springframework.batch.core.listener.StepExecutionListenerSupport;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.database.HibernateCursorItemReader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
public class RetrieveExtractParamsExectionContextReader extends StepExecutionListenerSupport implements ItemReader, ItemStream,
InitializingBean {
private HibernateCursorItemReader<ExtractParamsVO> itemExtractParamsReader;
private HibernateCursorItemReader<ExtractFiltersVO> itemExtractFilterReader;
private SessionFactory sessionFactory;
private String toc;
public void beforeStep(StepExecution stepExecution) {
itemExtractParamsReader = new HibernateCursorItemReader();
itemExtractParamsReader
.setQueryString("from ExtractParamsVO where tocID='" + toc
+ "'");
itemExtractParamsReader.setSessionFactory(sessionFactory);
itemExtractFilterReader = new HibernateCursorItemReader();
itemExtractFilterReader.setQueryString("from ExtractFiltersVO where tocID = '" + toc + "'");
itemExtractFilterReader.setSessionFactory(sessionFactory);
callDelegateAfterPropertiesSet(this.itemExtractParamsReader);
}
private void callDelegateAfterPropertiesSet(HibernateCursorItemReader d) {
try {
d.afterPropertiesSet();
} catch (Exception e) {
throw new RuntimeException("Error initializing delegate reader.", e);
}
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(sessionFactory,
"Property [sessionFactory] is required and must be set.");
}
public ExtractFiltersVO read() throws Exception, UnexpectedInputException,
ParseException {
Assert.state(itemExtractParamsReader != null,"ItemReader not initialized. Make sure to register as a step execution listener.");
ExtractParamsVO epvo = itemExtractParamsReader.read();
if (epvo != null){
itemExtractFilterReader.setQueryString("from ExtractFiltersVO where tocID = '" + toc + "' and setName = '" + epvo.getSetName() + "'");
return itemExtractFilterReader.read();
}
else {
return null;
}
}
@Override
public void open(ExecutionContext executionContext)
throws ItemStreamException {
itemExtractParamsReader.open(executionContext);
itemExtractFilterReader.open(executionContext);
}
@Override
public void update(ExecutionContext executionContext)
throws ItemStreamException {
itemExtractParamsReader.update(executionContext);
itemExtractFilterReader.update(executionContext);
}
@Override
public void close() throws ItemStreamException {
// TODO Auto-generated method stub
itemExtractParamsReader.close();
itemExtractFilterReader.close();
}
public String getToc() {
return toc;
}
public void setToc(String toc) {
this.toc = toc;
}
}