Job Chaining using quartz in Java
Asked Answered
P

2

9

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();
Plafond answered 3/8, 2017 at 7:38 Comment(0)
P
8

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();
Plafond answered 8/8, 2017 at 7:31 Comment(2)
I think your job must be durable to add it to the scheduler : https://mcmap.net/q/1315673/-how-to-mark-job-as-durable-in-quartz-netContaminant
The trigger can be simplified with TriggerBuilder.newTrigger().startNow().build()Liaoyang
L
3

I used Sachin answer to create this one:

public class Main {

public static void main(String[] args) throws SchedulerException {
    JobDetail jobA = JobBuilder.newJob(JobA.class).build();
    JobDetail jobB = JobBuilder.newJob(JobB.class).storeDurably(true).build();

    JobChainingJobListener jobListener = new JobChainingJobListener("ChainListener");
    jobListener.addJobChainLink(jobA.getKey(), jobB.getKey());

    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.scheduleJob(jobA, TriggerBuilder.newTrigger().startNow().build());
    scheduler.addJob(jobB, true);
    scheduler.getListenerManager().addJobListener(jobListener);
    scheduler.start();
}

public static class JobA implements Job {
    @Override
    public void execute(JobExecutionContext context) {
        System.out.println("Job A running...");
    }
}

public static class JobB implements Job {
    @Override
    public void execute(JobExecutionContext context) {
        System.out.println("Job B running...");
    }
}}

Working sample on GitHub: https://github.com/topera/poc-quartz-chain

Liaoyang answered 4/5, 2020 at 20:11 Comment(1)
How to handle case with more than 2 jobs? Like this #62727919Fluorescein

© 2022 - 2024 — McMap. All rights reserved.