[Spring Batch] (For forgot) What ItemReader should do

ItemReader get batch target object.

T read() method called until it returns null.
Therefore Spring batch would do something like below.

  1. Read record(s) by ItemReader.
  2. Do something at ItemProcessor.
  3. Write results at ItemWriter. When writing results, set flag or delete source data so that ItemReader would not to read same data again.

Or you can do like below. But you should be aware of exception handling.

  1. Read all target records and cached them at ItemReader.Then ItemReader returns one(or some) record(s).
  2. Do something at ItemProcessor.
  3. Write something at ItemWriter.

In this case, ItemReader keep records and current position.
To do this, you must define the ItemReader with scope=”step” in Bean Configuration.
scope=”step” means instance lifecycle is Step.
So you can keep current position as class member and it keeps until ItemReader returns null.







Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.