7

I am having two jobs(Job1,Job2) Job2 is depended on the results of job1 so it has to wait till job 1 finished

I need to chain them somehow:

  1. When job1 finish it needs to call job2. how to do that? using tasklet in the end which will call job2?

  2. The other way could be that the invoker(which is some scheduler service) will be responsible for calling job2 as soon as job1 returns - not really good because than ill need to call job1 synchronously.

How would you implement two chained (and depended jobs) using spring batch?

Thank you.

rayman
  • 20,786
  • 45
  • 148
  • 246
  • Possible duplicate of [Calling another job from a processor in spring batch](http://stackoverflow.com/questions/26575177/calling-another-job-from-a-processor-in-spring-batch) – Stewart Apr 09 '17 at 12:18

2 Answers2

7

You can use a JobStep to launch the second job from within the first job. See 5.3.6 Externalizing Flow Definitions and Dependencies Between Jobs

Jimmy Praet
  • 2,230
  • 1
  • 15
  • 14
  • Ah that might do the work. but do you think I should chain jobs this way or delegate that responsibility to the scheduler ? – rayman Jan 18 '15 at 22:08
  • It depends on how you want them to run. If you need them to be independent (run in different threads) than you have to find a way to persist the output of the first job to be processed by the 2nd. If you are ok with them running synchronously, than the JobStep is an easy solution for you. – Cristian Sevescu Jan 19 '15 at 12:37
0

This is my way of launching two (a list) jobs one after the other:

1- Declare the two job beans with @Order

@Order(1) // The first in the List
@Bean(name = "firstJob")
public Job firstJob() { .... }

@Order(2) // Second job to be launched
@Bean(name = "secondJob")
public Job secondJob() { .... }

2- Inject the list of jobs

@Autowired
List<Job> jobs;

3- Launch them

    public void run(String... args) {
    JobParameters params = new JobParametersBuilder()
            .addString("JobID", String.valueOf(System.currentTimeMillis()))
            .toJobParameters();
    jobs.forEach(job -> {
        try {
            jobLauncher.run(job, params);
        } catch (Exception e) {
            logger.error("Job {} cannot be executed", job.getName());
            e.printStackTrace();
        }
    });
}

I hope this helps new people reading this post

Youness Marhrani
  • 1,084
  • 16
  • 8