简体   繁体   中英

Sequential Job Schedule using Quartz in Spring

Sequential Job Schedule using Quartz in Spring

I have three or more jobs which depends on their respective previous jobs and they will run in the sequential order. If Job 1 finished Job 2 run and when Job 2 finished Job 3 run. If any error occurred in previous Job then next triggered Jobs will not be fired. I tried to know about Job-chaining using quartz but can't able to get it through.

The Jobs sequence are as per below

Job 1 -> Job 2 -> Job 3

we defined their respective beans in quartz.xml as per below

<beans ...>
    <bean name="scheduler1"
            class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
                <property name="jobClass"
                    value="com.abc.xyz.schedular.Scheduler1" />
                <property name="durability" value="true" />
            </bean>
            <bean id="trigger1"
            class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
                <property name="jobDetail" ref="scheduler1" />
                <property name="cronExpression" value="${scheduler1.cronExpression}" />
            </bean>
          <bean name="scheduler2"
            class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
                <property name="jobClass"
                    value="com.abc.xyz.schedular.Scheduler2" />
                <property name="durability" value="true" />
            </bean>
            <bean id="trigger2"
            class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
                <property name="jobDetail" ref="scheduler2" />
                <property name="cronExpression" value="${scheduler2.cronExpression}" />
            </bean>
            <bean name="scheduler3"
            class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
                <property name="jobClass"
                    value="com.abc.xyz.schedular.Scheduler3" />
                <property name="durability" value="true" />
            </bean>
            <bean id="trigger3"
            class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
                <property name="jobDetail" ref="scheduler3" />
                <property name="cronExpression" value="${scheduler3.cronExpression}" />
            </bean>
        <bean name="jobScheduler"
            class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="triggers">
                <list>
                    <ref bean="trigger1" />
                    <ref bean="trigger2" />
                    <ref bean="trigger3" />
                </list>
            </property>
            <property name="autoStartup" value="${jobScheduler.autoStartup}" />
        </bean>
</beans>

In the above xml files the cron expressions are as follows scheduler1.cronExpression =0 1 0 * * ? , scheduler2.cronExpression =0 2 0 * * ? , scheduler3.cronExpression =0 3 0 * * ? and the jobScheduler.autoStartup =true .

The Schedular class for Scheduler1.java , Scheduler2.java , Scheduler3.java is defined below.

public class Scheduler1 implements Job {

    private static final Logger log = (Logger) LogManager
            .getLogger(Scheduler.class.getName());

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        try {
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            /*
             *  doing my heavy Process
             */
        } catch (Exception e) {
            log.error("Error in setExpiredAllPlans Schedular1 ");
        }
    }
}


public class Scheduler2 implements Job {

    private static final Logger log = (Logger) LogManager
            .getLogger(Scheduler.class.getName());

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        try {
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            /*
             *  doing my heavy Process
             */
        } catch (Exception e) {
            log.error("Error in Schedular2 ");
        }
    }
}


public class Scheduler3 implements Job {

    private static final Logger log = (Logger) LogManager
            .getLogger(Scheduler.class.getName());

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        try {
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            /*
             *  doing my heavy Process
             */
        } catch (Exception e) {
            log.error("Error in setExpiredAllPlans Schedular3 ");
        }
    }
}

Now how would i able to chain the jobs using above configuration because sometimes these heavy process take more than 1 minute so i don't want to start the next fire job until previous job is completed. If you have any concern/clarification ,please revert me.

Thanks in advance.

To chain the execution of your jobs, you will need to implement a JobListener . In its jobWasExecuted event handler you will need to define your job chaining logic so that your jobs are executed in a sequential order regardless of how long the executions of individual jobs take. In that case you will only need a trigger for the very first job (Job 1) as the chained jobs (Job 2 and Job 3) will be triggered "manually" from the event handler.

You can also use the JobChainingJobListener implementation that enables you to programmatically define simple job chains in your application. To define your job chain, you will use the addJobChainLink method.

If you would like a robust solution that will allow you to avoid hard-coding the job chaining logic in your application code while letting you modify your job chaining rules at runtime through a GUI without the need to touch, redeploy and restart your application, you can consider using our product QuartzDesk . QuartzDesk comes with a powerful job chaining engine that allows you to implement all sorts of common job chaining scenarios including scheduled (ie not immediate) executions of your chained (target) jobs, multiple target jobs, local/remote target jobs etc.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM