I wrote a class that extends StoredProcedureItemReader in the following way:
@Component
public class SpFirstReader extends StoredProcedureItemReader<StudentResults> implements StepExecutionListener {
private static final String GET_SP_NAME = "univ.test";
private final DataSource dataSource;
private int param;
@PostConstruct
public void initialize() {
super.setDataSource(dataSource);
}
SpReader(DataSource dataSource) {
this.dataSource = dataSource;
setName("SpReader");
setDataSource(this.dataSource);
setProcedureName(GET_SP_NAME);
setParameters(new SqlParameter[] { new SqlParameter("@rollno", java.sql.Types.INTEGER) });
setPreparedStatementSetter(ps -> ps.setInt(1, param));
setRowMapper(new SpObRowMapper());
}
@Override
public void beforeStep(StepExecution stepExecution) {
param = stepExecution.getJobExecution().getExecutionContext().getInt("param");
}
}
The code works fine. Now I have a scenario where I want to check for a condition and based on that condition I will be deciding the Procedure name.
But the problem here is: the procedure name (defined in setProcedureName()) is not getting modified based on a boolean condition on runtime as the bean is getting created on startup. Due to this when I call the reader the boolean condition to dynamically allocate the procedure name is skipped and just executing with the procedure name that gets defined and allocated on the startup.
To circumvent this, I tried creating a second ItemReader with the other procedure name and tried validating the boolean condition by implementing a customItemReader in the following way:
@Component
@RequiredArgsConstructor
public class CustomItemReader<T> implements ItemReader<T> {
private final Utils utils;
private boolean statusCheck() {
String status = utils.getLastStatus();
return Objects.equals(status, "COMPLETED");
}
@Override
public T read()
throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
if (!this.statusCheck()) {
return (T) new SpSecondReader(null);
}
return (T) new SpFirstReader(null);
}
}
Adding the above code and calling the CustomItemReader from the step configuration, the code is running the read() method in a never ending loop on CustomItemReader()
Any help in fixing the issue where I can have a dynamic procedure name (under one reader) or multiple readers that can be called based on the condition defined in statusCheck() on CustomItemReader() class (without the infinite loop that I am currently running into) would be deeply appreciated.
Thanks in advance!