Java Reference
In-Depth Information
Using nothing more that log4j, you can get the input that failed to parse from the
FlatFileParseException and log it to your log file. However, this by itself does not accomplish your goal of
logging the error record to a file and continuing on. In this scenario, your job will log the record that
caused the issue and fail. In the last section, you will look at how to handle having no input when your
jobs run.
Dealing with No Input
A SQL query that returns no rows is not an uncommon occurrence. Empty files exist in many situations.
But do they make sense for your batch process? In this section, you will look at how Spring Batch
handles reading input sources that have no data.
When a reader attempts to read from an input source and a null is returned the first time, by default
this is treated like any other time a reader receives a null; it considers the step complete. While this
approach may work in the majority of the scenarios, you may need to know when a given query returns
zero rows or a file is empty.
If you want to cause your step to fail or take any other action (send an e-mail, etc) when no input
has been read, you use a StepListener. In Chapter 4, you used a StepListener to log the beginning and
end of your step. In this case, you can use the StepListener's @AfterStep method to see how many
records were read and react accordingly. Listing 7-67 shows how you would mark a step failed if no
records were read.
Listing 7-67. EmptyInputStepFailer
package com.apress.springbatch.chapter7;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.annotation.AfterStep;
public class EmptyInputStepFailer {
public ExitStatus afterStep(StepExecution execution) {
if(execution.getReadCount() > 0) {
return execution.getExitStatus();
} else {
return ExitStatus.FAILED;
To configure your listener, you configure it like you would any other StepListener. Listing 7-68
covers the configuration in this instance.
Listing 7-68. Configuring the EmptyInputStepFailer
<beans:bean id="emptyFileFailer"
<step id="copyFileStep">
Search WWH ::

Custom Search