2

I have a spring batch application which reads data from a DB table with JdbcCursorItemReader and writes it to a flat file with FlatFileItemWriter.

When I test my application, I see that the FlatFileItemWriter creates a file even if no data is returned from the DB via JdbcCursorItemReader. However, I'm planning to fail my job when there is no appropriate data in DB. Is it possible to do so or at least prevenet FlatFileItemWriter from creating a file?

Regards

Kivanc
  • 246
  • 1
  • 7
  • 13

4 Answers4

5

from http://static.springsource.org/spring-batch/reference/html/patterns.html

11.7. Handling Step Completion When No Input is Found

In many batch scenarios, finding no rows in a database or file to process is not exceptional. The Step is simply considered to have found no work and completes with 0 items read. All of the ItemReader implementations provided out of the box in Spring Batch default to this approach. This can lead to some confusion if nothing is written out even when input is present. (which usually happens if a file was misnamed, etc) For this reason, the meta data itself should be inspected to determine how much work the framework found to be processed. However, what if finding no input is considered exceptional? In this case, programmatically checking the meta data for no items processed and causing failure is the best solution. Because this is a common use case, a listener is provided with just this functionality:

public class NoWorkFoundStepExecutionListener extends StepExecutionListenerSupport {

public ExitStatus afterStep(StepExecution stepExecution) {
    if (stepExecution.getReadCount() == 0) {
        return ExitStatus.FAILED;
    }
    return null;
}

}
Trever Shick
  • 1,734
  • 16
  • 17
  • **All of the ItemReader implementations provided out of the box in Spring Batch default to this approach** , I am using `JdbcPagingItemReader` and it marks job as failed if reader doesn't have any rows to begin with. Am I missing something? – Sabir Khan Aug 29 '16 at 10:32
  • I also added listener as described in your answer and registered with step but job is still marked as failed. For my scenario, no rows is a valid situation. – Sabir Khan Aug 29 '16 at 10:39
  • @SabirKhan I don't think so, I use a JdbcCursorItemReader and I don't get any exception when there is no data to read. – pmartin8 Jan 24 '20 at 20:55
1

Is it possible to do so or at least prevent FlatFileItemWriter from creating a file?

For the second part your of your question FlatFileItemWriter has a flag to delete the file if nothing is written.

shouldDeleteIfEmpty : Flag to indicate that the target file should be deleted if no lines have been written (other than header and footer) on close.

Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
0

ItemReadListener's afterRead method will be called even if there is no data. You can probably put what you wanted over there.

Ramesh
  • 3,841
  • 5
  • 22
  • 28
0

ItemProcessor will only be called if the ItemReader returns an object. Null value is used to indicate processing is finished.

Do not use null value as an error indication : it's purpose is to end the processing. You would better use an exception for your errors.

Jean-Philippe Briend
  • 3,455
  • 29
  • 41