Skip to content

Add PageableItemReader - #5477

Closed
scordio wants to merge 4 commits into
spring-projects:mainfrom
scordio:gh-910-pageable-item-reader
Closed

scordio wants to merge 4 commits into
spring-projects:mainfrom
scordio:gh-910-pageable-item-reader

Conversation

@scordio

@scordio scordio commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

This PR adds PageableItemReader, a type-safe RepositoryItemReader alternative that uses a Function<Pageable, Slice<?>> instead of the reflection-based strategy for query invocation.

It also adds a related PageableItemReaderBuilder.


PageableItemReader is deliberately designed after RepositoryItemReader in its paging, restart, and locking behavior. However, it favors constructor parameters instead of setters for mandatory configuration.

In addition, compared to RepositoryItemReader, there are two improvements:

  • Expects an org.springframework.data.domain.Sort at construction time instead of computing it inside doPageRead()
  • Simplifies concurrency by relying only on lock-based visibility/atomicity, as volatile field modifiers were redundant with the existing ReentrantLock

Lastly, I preferred introducing a dedicated reader rather than enhancing RepositoryItemReader to support both use cases (following the rationale at #4484 (comment)). However, if you'd prefer to enhance RepositoryItemReader, let me know and I'll take care of the changes.


@scordio
scordio force-pushed the gh-910-pageable-item-reader branch from cd8583d to e925b29 Compare September 30, 2026 23:46
@fmbenhassine

Copy link
Copy Markdown
Contributor

Thank you for this addition! A separate reader is the way to go. However, I wouldn't deprecate RepositoryItemReader though, please revert the changes related to that. I added a couple inline comments for minor changes.

Another point I wanted to share about duplicated logic: there are several lines of page, restart and lock handling that are similar to RepositoryItemReader. Since we are going to keep both readers, would it make sense to try to move the common logic to an abstract base class?

@scordio
scordio force-pushed the gh-910-pageable-item-reader branch from e925b29 to 48316ec Compare October 6, 2026 10:06
@scordio

scordio commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

I wouldn't deprecate RepositoryItemReader though, please revert the changes related to that.

Done in 48316ec.

Another point I wanted to share about duplicated logic: there are several lines of page, restart and lock handling that are similar to RepositoryItemReader. Since we are going to keep both readers, would it make sense to try to move the common logic to an abstract base class?

Indeed, I'm on it!

@scordio

scordio commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

I started working on a new AbstractPageableItemReader and noticed that AbstractPagingItemReader and AbstractPaginatedDataItemReader already exist, both with page, restart and lock handling very similar to RepositoryItemReader.

However:

  • AbstractPaginatedDataItemReader could be a good candidate for PageableItemReader, but not for RepositoryItemReader due to doPageRead() returning an Iterator<T> instead of List<T> and not declaring checked exceptions, so it would be a breaking change for it.
  • AbstractPagingItemReader advances based on the configured page size, not the returned content size or hasNext() result, so not a good option for either PageableItemReader or RepositoryItemReader.

I don't think I should introduce a third abstraction. However, I'm not sure how to solve the duplication in RepositoryItemReader without either a breaking change or one more abstract class variant. I tend to lean toward adopting AbstractPaginatedDataItemReader only for PageableItemReader, leaving RepositoryItemReader as is.

@fmbenhassine WDYT?

Maybe version 7 could merge AbstractPagingItemReader and AbstractPaginatedDataItemReader into a single abstraction.

@fmbenhassine

Copy link
Copy Markdown
Contributor

No need for a third abstraction. I agree on adopting AbstractPaginatedDataItemReader only for PageableItemReader, leaving RepositoryItemReader as is.

@scordio

scordio commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

While adopting AbstractPaginatedDataItemReader, I think I stumbled across a bug, so I raised a separate PR for it:

Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
…date docs

Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
@scordio
scordio force-pushed the gh-910-pageable-item-reader branch from 48316ec to 5b8ca9d Compare October 11, 2026 14:16
@scordio

scordio commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor Author

I updated this PR assuming #5583 will be merged (PageableItemReaderTests#testSettingCurrentItemCountExplicitly fails because this branch is missing the changes from that PR – happy to rebase this one later on).

@fmbenhassine

Copy link
Copy Markdown
Contributor

Rebased, squashed and merged as a1b0946. I omitted a couple unrelated changes that we can apply in a separate PR.

Thank you very much for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve JPA repository readers to be more typesafe [BATCH-2697]

2 participants