Skip to content

CompositeItemReader Does Not Reset Iterator for Subsequent Job Executions #4926

@ahosni

Description

@ahosni

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

  1. Create a Spring Batch job that uses CompositeItemReader with multiple delegate readers.
  2. Launch the job in a scheduled method using : jobLauncher.run(job, jobParameters);
  3. Allow the scheduled method to trigger the job a second time.
  4. 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;
	}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions