Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 780
Yunus Einsteinium Avatar asked Nov 25 '25 11:11

Yunus Einsteinium


1 Answers

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.

like image 55
Sabir Khan Avatar answered Nov 27 '25 23:11

Sabir Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!