Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring batch: restarting a completed job with same parameters

Tags:

spring-batch

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 ?

like image 617
Vicky Avatar asked Feb 01 '12 13:02

Vicky


People also ask

Is it possible to restart a completed job in Spring Batch?

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.

How do you repeat a job in Spring Batch?

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.

How do I rerun a batch 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.


3 Answers

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);
like image 163
Jijo Avatar answered Oct 07 '22 08:10

Jijo


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)

like image 22
user1201659 Avatar answered Oct 07 '22 08:10

user1201659


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

like image 31
Serkan Arıkuşu Avatar answered Oct 07 '22 08:10

Serkan Arıkuşu