Description
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
- Create a Step with custom ItemWriter implementation.
- Give the chunk size of the step as 10.
- Launch the job with 10-20 items (for example 16).
- 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!");
}
}
}