-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
Bug description
The CompositeItemReader implementation has an issue where the iterator delegatesIterator
does not reset after the first execution of a job. This causes subsequent executions of the job (e.g., in a scheduled method) to skip calling the delegate readers because the iterator remains at the end.
Environment
Spring Batch version : 5.2.2
Steps to reproduce
- Create a Spring Batch job that uses
CompositeItemReader
with multiple delegate readers. - Launch the job in a scheduled method using : jobLauncher.run(job, jobParameters);
- Allow the scheduled method to trigger the job a second time.
- Observe that the
CompositeItemReader
does not call the delegate readers again because the iterator is at the end.
Expected behavior
The CompositeItemReader
should reset its iterator and call the delegate readers for each job execution, regardless of whether it is the first or subsequent execution.
Proposed Solution
Modify the open
method of CompositeItemReader
to reset the iterator delegatesIterator
and reinitialize the currentDelegate
to the first reader. This ensures that the iterator starts fresh for each job execution.
@Override
public void open(ExecutionContext executionContext) throws ItemStreamException {
for (ItemStreamReader<? extends T> delegate : delegates) {
delegate.open(executionContext);
}
initIteratorAndCurrentDelegate();
}
private void initIteratorAndCurrentDelegate() {
this.delegatesIterator = this.delegates.iterator();
this.currentDelegate = this.delegatesIterator.hasNext() ? this.delegatesIterator.next() : null;
}