Java Reference
In-Depth Information
Controlling Restart
Spring Batch provides many facilities to address stopping and restarting a job, as you've seen. However,
it's up to you to determine what can and can't be restarted. If you have a batch process that imports a file
in the first step ,and that job fails in the second step, you probably don't want to reimport the file. There
are scenarios where you may only want to retry a step a given number of times. This section looks at how
to configure a job to be restartable and how to control how it's restarted.
Preventing a Job from Being Rerun
All the jobs up to now could be executed again if they failed or were stopped. This is the default behavior
for Spring Batch. But what if you have a job that can't be rerun? You give it one try, and if it works, great.
If not, you don't run it again. Spring Batch provides the ability to configure jobs to not be restartable
using the restartable attribute of the job tag.
If you look at the transactionJob configuration, by default the restartable attribute is true .
However, if you choose to configure it to be false , as shown in Listing 6-36, then when the job fails or is
stopped for any reason, you won't be able to reexecute it.
Listing 6-36. transactionJob Configured to Not Be Restartable
...
<job id="transactionJob" restartable="false">
<step id="step1" parent="importTransactionFileStep" next="step2"/>
<step id="step2" parent="applyTransactionsStep" next="step3"/>
<step id="step3" parent="generateAccountSummaryStep"/>
</job>
...
Now if you attempt to run the job after a failure, you're told by Spring Batch that the JobInstance
already exists and isn't restartable, as shown in Listing 6-37.
Listing 6-37. Results from Reexecuting a Non-Restartable Job
2011-01-13 19:50:30,559 ERROR
[org.springframework.batch.core.launch.support.CommandLineJobRunner] - <Job
Terminated in error: JobInstance already exists and is not restartable>
org.springframework.batch.core.repository.JobRestartException: JobInstance
already exists and is not restartable
at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJob
Launcher.java:97)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(Comm
andLineJobRunner.java:348)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(Comma
ndLineJobRunner.java:565)
Being able to execute a job once or else may be a bit extreme for some scenarios. Spring Batch also
lets you configure the number of times a job can be run, as you see next.
 
 
Search WWH ::




Custom Search