Skipped items written back in proper order
Hello.
I'm using Spring Batch to read a text file, to do some processing, and to write the line back to a new file with some status code (e.g., successfully processed, not processed).
There are exceptions that happen during the reading part, so I implemented a SkipListener and included the Exceptions as skippable-exception-classes. In the SkipListener's onSkipInRead I retrieve the line skipped and use the same writer above to write the line appended with the status code (let's say...) "Not processed".
Sample input:
Record 1|Apple
Record 2|Orange
Record 3|Banana
Expected output assuming that an exception occurred while reading Record 2:
Record 1|Apple|Successfully processed
Record 2|Orange|Not processed
Record 3|Banana|Successfully processed
However, in my implementation, the output was:
Record 1|Apple|Successfully processed
Record 3|Banana|Successfully processed
Record 2|Orange|Not processed
The order is no longer maintained. My understanding here is that the SkipListener follows the flow as stated in the javadoc:
Quote:
"Implementers of this interface should not assume that any method will be called immediately after an error has been encountered. Because there may be errors later on in processing the chunk, this listener will not be called until just before committing."
Thus, the skipped item was processed (i.e., written) only after the rest of the chunk were written. The next thing I tried was to use ItemReadListener's onReadError(Exception ex), but the order was not right as well as I would have expected:
Record 2|Orange|Not processed
Record 1|Apple|Successfully processed
Record 3|Banana|Successfully processed
My chunk's commit-interval is set to 10. What are possible solutions to maintain the same order in the output file during skips?
Thanks!