Skip to content

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

Closed
@myanar7

Description

@myanar7

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!");
    }
   }
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions