0

I am using Spring Batch Admin to run batch jobs for our system. We have to run these jobs many times. I am using the rest interface of spring batch admin. I start a new job using Spring RestTemplate.

RestTemplate tpl = new RestTemplate();
tpl.postForLocation(Constants.SPRING_BATCH_ADMIN_URL + "/jobs/myBatchJob;

This works to start the first job but on subsequent jobs the request does not start a new instance of the job. In the job configuration file I have an jobParmatersIncrementer defined

<job id="myBatchJob" xmlns="http://www.springframework.org/schema/batch"
    restartable="true" incrementer="jobParametersIncrementer">
    <bean id="jobParametersIncrementer"
        class="org.springframework.batch.core.launch.support.RunIdIncrementer" />

I have tried changing my postForLocation to

List<JobInstance> jobInstances = jobExplorer.getJobInstances("myBatchJob", 0, 30);
JobParameters jobParameters = jobInstances.get(0).getJobParameters();
RunIdIncrementer runIdIncrementer = new RunIdIncrementer();
JobParameters jobParameters = runIdIncrementer.getNext(jobParameters); 
RestTemplate tpl = new RestTemplate();
tpl.postForLocation(Constants.SPRING_BATCH_ADMIN_URL + 
    "/jobs/myBatchJob?launch=Launch&{parameters}",
    "runid",
    jobParameters.toString());

It works from the spring batch admin page by clicking the launch button. This is in the parameter edit box

run.id(long)=1

How do you run a job in Spring Batch Admin from another web application more than once?

JG in SD
  • 5,427
  • 3
  • 34
  • 46
jmclurkin
  • 11
  • 3

1 Answers1

0

You can pass the a parameter jobParameter in the request body, that carries your tuples

jobParameters=run.id(long)=123

and attach it url encoded to the post request. Here is my jquery solution.

$.post('<url>/jobs/myBatchJob', 'jobParameters=' + encodeURIComponent('run.id(long)=123')).done(function() {
   // do something
});
damoeb
  • 154
  • 3
  • 9