Abhijeet Mahapatro
Abhijeet Mahapatro

Reputation: 3

Run Spring Batch Jobs in sequence

I have a spring batch job Ex: Job2 which runs in partitions. The primary job takes about 1-2 hours to complete.

I am trying to introduce a precondition which is a new Job Job1 (And it has to be a Job, can not be a step) and it should execute before the Job Job2. This also takes 20-30 mins to execute, basically it refines the data which was being processed as raw from long time.

@Bean
public Job job1() {
    return this.jobBuilderFactory.get("Job1").listener(jobListener()).incrementer(new RunIdIncrementer())
            .start(anotherStep()).build();
}

@Bean
public Job job2() {
    Step step = stepBuilderFactory.get("Job2Step").listener(readListener()).listener(stepListener())
            .listener(writeListener()).<Item, Item>chunk(1).reader(jobReader()).processor(jobProcessor())
            .writer(jobWriter()).build();

    return jobBuilderFactory.get("Job2").listener(jobListener()).start(step).build();
}

if (Job1 is running) { //Pause Job2 }

As I don't want to change existing functionality in the primary job, Can we pause the primary job Job2 until the main Job1 is not completed. Note: These jobs get triggered every 3-4 hours

Upvotes: 0

Views: 731

Answers (1)

johnnyutts
johnnyutts

Reputation: 1452

You can use JobStep so you have a parent job that runs job1 and then job2. Something like this

    @Bean
    public Job jobStepJob(Step jobStep1, Step jobStep2) {
        return this.jobBuilderFactory.get("jobStepJob")
                .start(jobStep1)
                .next(jobStep2)
                .build();
    }

    @Bean
    public Step jobStep1(Job job1) {
        return this.stepBuilderFactory.get("jobStep1")
                .job(job1)
                .build();
    }

    @Bean
    public Step jobStep2(Job job2) {
        return this.stepBuilderFactory.get("jobStep2")
                .job(job2)
                .build();
    }

Upvotes: 1

Related Questions