Yunus Einsteinium
Yunus Einsteinium

Reputation: 1180

Spring Batch - Rerun Job with same Job Parameter

Spring Batch new guy here,so, expect anything.

I have a job that runs successfully to completion with given job parameters paymentPeriod=1. Though the requirement expects the job to be able to rerun with same job parameters paymentPeriod=1 .

I can run job first time with parameter paymentPeriod=1 with following endpoint from postman

@RestController
@RequestMapping(value = "api/jobs")
public class JobController {
    @Autowired
    private JobOperator jobOperator;

    @RequestMapping(value = "/pay/{paymentPeriod}", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void launchPaymentJob(@PathVariable Integer paymentPeriod) throws Exception {
        this.jobOperator.start("paymentJob", String.format("paymentPeriod=%s", paymentPeriod));
    }
}

Though when i rerun with same parameters i get JobInstanceAlreadyExistsException cannot start a job instance that already exists with name paymentJob and parameters=paymentPeriod=1

Upvotes: 3

Views: 5784

Answers (1)

Sabir Khan
Sabir Khan

Reputation: 10142

Going by the concept of Job Instances and Job Executions in Spring Batch, you can't start a COMPLETED job instance again though you can launch same instance of job again & again till its not COMPLETE ( and few more job statuses ).

Job instance uniqueness is achieved by jobId & job parameters.

So if you don't vary your parameters, you can't start a completed job with same instance.

If your REST End Point is restricted to take same value again and again, you need to add a unique parameter within that method and that can very well be - java.lang.System.currentTimeMillis().

You can use any other unique value though current system time is very convenient.

Convert java.lang.System.currentTimeMillis() to String and append to your parameters with something like , String.format("paymentPeriod=%s", paymentPeriod+java.lang.System.currentTimeMillis()) instead of what you are currently.

Hope you get the idea.

Upvotes: 3

Related Questions