Reputation: 2687
I have a spring batch application using spring-boot (no xml configuration). I'm trying to understand the usefulness of ExitStatus in my application. At the end of one of the steps, I need to execute an SQL update statement. I achieve this by having a StepExecutionListener like below:
@Component
public class MyListener extends StepListenerSupport{
@Autowired
JdbcTemplate jdbcTemplate;
@Transactional
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
jdbcTemplate.execute("some SQL update);
return null;
}
}
As you can see, I return null
as ExitStatus and this appears to work fine. However, I'm not sure if this is the right thing to do or if I need to return any specific value for ExitStatus?
Upvotes: 0
Views: 3774
Reputation: 3868
Returning null
here effectively says "do not override the ExitStatus
you already were going to use".
The usefulness comes in because, if your SQL statement fails (and presumably throws an exception), the step WILL NOT FAIL. Instead it will keep going happily to the next step in your job.
You need to catch the exception and return ExitStatus.FAILED
to fail the step and the job.
Alternatively, let's say you want have a scenario where you want to do some flow control based on the activity in the step... maybe it was a staging step and you want to end if there were no records to process but do some additional step if there were records to stage. Then you might do something like this:
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
if (stepExecution.getReadCount() > 0) {
return ExitStatus.COMPLETED;
}
return ExitStatus.NOOP;
}
Then in your job configuration you might do:
jobBuilderFactory.get("myJob")
.start(step1()).on("NOOP").end()
.from(step1()).on("COMPLETED").to(step2()).end()
.from(step1()).on("*").fail()
.build();
Upvotes: 3