Announcement Announcement Module
Collapse
No announcement yet.
Running multiple jobs in spring batch admin using REST with same jobname. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Running multiple jobs in spring batch admin using REST with same jobname.

    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.

    Code:
    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
    Code:
    <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
    Code:
    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?

  • #2
    I haven't tried with the resttemplate but let me tell you what I tried and worked for me.

    I created a Tasklet which will clear the spring batch tables -> RemoveSpringBatchHistoryTasklet http://tinyurl.com/adpnzg6 (Courtesy Antoine Rey).

    The tasklet is been added as a new job, since I don't want to interrupt the existing job.

    Code:
    <job id="clearBatchJob" xmlns="http://www.springframework.org/schema/batch" restartable="true">
    
        <step id="clearBatchJobStep"> <!-- Step 0 -->
        <!-- Basic step to clear all batch related table entries so that batch job can run again -->
            <tasklet ref="removeSpringBatchHistoryTasklet"/>
        </step>     
    </job>
    <bean id="removeSpringBatchHistoryTasklet" class="com.myapp.test.tasklet.RemoveSpringBatchHistoryTasklet" />
    Then I ran this job before running the original job.
    Code:
    RestTemplate restTemplate = new RestTemplate();
    
        restTemplate.postForLocation(Constants.SPRING_BATCH_ADMIN_URL + "/jobs/{jobName}.json", "name", clearBatchJobName);
    
        restTemplate.postForLocation(Constants.SPRING_BATCH_ADMIN_URL + "/jobs/{jobName}.json", "name", myBatchJobName);
    I know that this is not the best solution. But this worked for me

    Comment

    Working...
    X