Is it possible to restart a job in spring batch with same job params, which has completed successfully?
Say I have a job with a step which reads from one file and writes to another.
For test purpose, I need to run the job again and again. However, I do not want the job param (which is today's date which I am reading from a table) to change again and again.
Is such a scenario possible ?
By default, it does not allow completed jobs to be restarted. When a job is restarted, Spring Batch will create a new job execution for the particular job instance that failed, and it will restart at the failed step, executing from that point forward.
You can enable scheduling simply by adding the @EnableScheduling annotation to the main application class. Scheduling a task is as simple as annotating a method with @Scheduled annotation. In the below example, execute() method is scheduled to run every minute. execute() method will invoke the desired job.
To restart a Spring Batch Job, navigate to the Jobs page by clicking the Jobs tab located on the left hand side of the UI. Now identify the job you wish to restart. In our example (shown in the following image), JobTwo shows a status of FAILED . Thus, the UI provides an option to restart this batch job.
Spring Batch requires unique job parameters for its execution.so you can add the current time as a job parameter
Map<String, JobParameter> confMap = new HashMap<String, JobParameter>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);
jobLauncher.run(springCoreJob, jobParameters);
Long startNextInstance(String jobName)
throws NoSuchJobException, JobParametersNotFoundException, JobRestartException,
JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException;
This method of the JobOperator class along with a JobParameterIncrementer can be used to restart a job, either failed or completed.
http://static.springsource.org/spring-batch/apidocs/org/springframework/batch/core/launch/support/SimpleJobOperator.html#startNextInstance(java.lang.String)
Spring Batch requires unique job parameters for its execution. In your case, if you want to run the same job with the same date parameter, than you should "add" another job parameter to make it unique. You may think of it unique job parameter set.
org.springframework.batch.core.JobParametersIncrementer
interface can be used in this scenario, just give it your JobParameter and it will add a run.id that will make it unique.
public class SampleIncrementer implements JobParametersIncrementer {
public JobParameters getNext(JobParameters parameters) {
if (parameters==null || parameters.isEmpty()) {
return new JobParametersBuilder().addLong("run.id", 1L).toJobParameters();
}
long id = parameters.getLong("run.id",1L) + 1;
return new JobParametersBuilder().addLong("run.id", id).toJobParameters();
} }
You may check a simple sample using it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With