Skip to content

Incorrect Chunk property value in implementation of ItemWriter write method #4560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
myanar7 opened this issue Mar 4, 2024 · 3 comments
Closed
Labels
for: backport-to-5.0.x Issues that will be back-ported to the 5.0.x line for: backport-to-5.1.x Issues that will be back-ported to the 5.1.x line in: core type: bug
Milestone

Comments

@myanar7
Copy link
Contributor

myanar7 commented Mar 4, 2024

Bug description
I was implementing an ItemWriter class by overriding write method. I wanted to add a condition to do if the chunk is the last one. I saw that there is a isEnd() method in itself. But it never comes true even if the chunk is the last chunk.

I debugged the spring batch classes. end property of the actual chunk object is true when it is the last chunk. However, the chunk object are reproduced by the actual chunk. The reproduced chunk passing to ItemWrite write method. It does not preserve variables such as end.

I also opened a pull request for this problem to fix. A test case is included in PR.

Environment
Java Version: 21 (openjdk-21.0.2)
Spring Batch Core: 5.0.0 (also tried 5.2.0)
Spring Batch Infrastructure: 5.1.0

Steps to reproduce

  1. Create a Step with custom ItemWriter implementation.
  2. Give the chunk size of the step as 10.
  3. Launch the job with 10-20 items (for example 16).
  4. Print the chunk.isEnd() in the write method of the implementation.

Expected behavior
In the last call of write method, chunk.isEnd() should be true.

Minimal Complete Reproducible example

public class ConditionalItemWriter implements ItemWriter<String> {

  @Override
  public void write(Chunk<? extends String> chunk) {
    if (chunk.isEnd()) {
      System.out.println("This was the last chunk!");
    }
   }
  }
@fmbenhassine
Copy link
Contributor

Thank you for reporting this issue and opening a PR! The issue is valid. However, in the case where the number of items in the last chunk is equal to the configured commit interval, the property might be misleadingly false (this is because Spring Batch does not peek at the next chunk when the current chunk is ready, ie in that case there will be a last empty chunk that won't be processed). I think this should be mentioned in the javadoc of the method.

@myanar7
Copy link
Contributor Author

myanar7 commented Mar 7, 2024

Thank you for your feedback. Should we mentioned it in the javadoc of the Chunk's isEnd method or ItemWriter's write method? Can I include this change into the pull request?

@fmbenhassine
Copy link
Contributor

Yes, I think it is more appropriate to update the javadoc of Chunk#isEnd.

@fmbenhassine fmbenhassine added this to the 5.2.0 milestone Mar 14, 2024
@fmbenhassine fmbenhassine added for: backport-to-5.0.x Issues that will be back-ported to the 5.0.x line for: backport-to-5.1.x Issues that will be back-ported to the 5.1.x line labels Mar 14, 2024
@fmbenhassine fmbenhassine modified the milestones: 5.2.0, 5.2.0-M1 Sep 16, 2024
FBibonne pushed a commit to FBibonne/spring-batch that referenced this issue Feb 2, 2025
Resolves spring-projects#4560

Signed-off-by: Fabrice Bibonne <fabrice.bibonne@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: backport-to-5.0.x Issues that will be back-ported to the 5.0.x line for: backport-to-5.1.x Issues that will be back-ported to the 5.1.x line in: core type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants