Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Job Chaining using quartz in Java

I want to execute multiple jobs sequentially . I am trying it in following way but somehow its not gets triggered using cron job.

I am not sure whether its a correct implementation approach . Can someone please guide me on this ?

Thanks ..

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        JobChainingJobListener jobListener =new JobChainingJobListener("ChainListener");

        JobDetail job1 = JobBuilder.newJob(MyJob1.class)
                .withIdentity(MyJob1.class.getName()).
                withDescription(MyJob1.class.getName())
                .build();

        JobDetail job2 = JobBuilder.newJob(MyJob2.class)
                .withIdentity(MyJob2.class.getName()).
                withDescription(MyJob2.class.getName())
                .build();

        JobDetail job3 = JobBuilder.newJob(MyJob3.class)
                .withIdentity(MyJob3.class.getName()).
                withDescription(MyJob3.class.getName())
                .build();           

        Trigger jobTrigger = TriggerBuilder.newTrigger()
                .withIdentity("MyTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule(cronTrigger)).build();
        scheduler.scheduleJob(job1, jobTrigger);    
        scheduler.addJob(job2, true);
        scheduler.addJob(job3, true);               

        jobListener.addJobChainLink(job1.getKey(), job2.getKey());
        jobListener.addJobChainLink(job2.getKey(), job3.getKey());
        scheduler.getListenerManager().addJobListener(jobListener);
        scheduler.start();
like image 813
Sachin Avatar asked Aug 03 '17 07:08

Sachin


Video Answer


1 Answers

After adding group name to job and trigger identity able to execute it . Here is correct code :

    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    JobChainingJobListener jobListener =new JobChainingJobListener("ChainListener");

    JobDetail job1 = JobBuilder.newJob(MyJob1.class)
            .withIdentity(MyJob1.class.getName(),"BatchJobGroup").
            withDescription(MyJob1.class.getName())
            .build();

    JobDetail job2 = JobBuilder.newJob(MyJob2.class)
            .withIdentity(MyJob2.class.getName(),"BatchJobGroup").
            withDescription(MyJob2.class.getName())
            .build();

    JobDetail job3 = JobBuilder.newJob(MyJob3.class)
            .withIdentity(MyJob3.class.getName(),"BatchJobGroup").
            withDescription(MyJob3.class.getName())
            .build();           

    Trigger jobTrigger = TriggerBuilder.newTrigger()
            .withIdentity("MyTrigger","BatchJobGroup").
            .withSchedule(CronScheduleBuilder.cronSchedule("* * * * *")).build();

    scheduler.scheduleJob(job1, jobTrigger);    
    scheduler.addJob(job2, true);
    scheduler.addJob(job3, true);               

    jobListener.addJobChainLink(job1.getKey(), job2.getKey());
    jobListener.addJobChainLink(job2.getKey(), job3.getKey());
    scheduler.getListenerManager().addJobListener(jobListener);
    scheduler.start();
like image 116
Sachin Avatar answered Oct 06 '22 11:10

Sachin