Announcement Announcement Module
No announcement yet.
How do I setup a step that has special behavior in the case of an empty data set? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I setup a step that has special behavior in the case of an empty data set?

    I have a spring batch job that is taking entities from one database table and synchronizing them with entities in another table in a 1 to 1 fashion. So, if an entity is removed from the source table, the job will remove it from the destination table in the item writer.

    This works fine when is at least 1 entry in the source table, however it fails when there are 0 entries. I know the reason for this is that the item reader (which is a org.springframework.batch.item.database.JdbcCursor ItemReader) is returning null after the first call to read(). This in turn is causing no chunks to be created/processed.

    The problem here is that in this case, I want the item writer to be called with an empty list rather than not be called at all because I want the destination table to be cleared out as well. Is there any way I can do this? I've looked at adding listeners, creating a custom chunk provider, etc, but all these ways seem wrong.

  • #2
    I don't see any way to do this cleanly. The only things I have in mind is to use a step listener and look-up the metadata, something like:

        public ExitStatus afterStep(StepExecution stepExecution) {
            if (stepExecution.getReadCount() == 0) {
                  // Invoke logic when no item was read
            return null;
    On your item writer. Beware about the transaction demarcation here.


    • #3
      Thanks very much for your response. This seems like the best way to deal with the issue.