jmao
jmao

Reputation: 33

clustered quartz instances schedule duplicate jobs?

I have a Tomcat web application A that schedules jobs with quartz framework. It was running as one instance on RAM Jobstore. Recently we plan to move to JDBC Jobstore and set up a cluster, so that if one instance fails, the other instance will be able to run the job.

Here is my issues:

My we application A will scan a business related database table and schedule quartz jobs during starting up and every 10 minutes afterwards. If I deploy two A applications on two different tomcat instances, then there will be two sets of duplicate jobs scheduled through Quartz.

How should I solve this problem? Do I need to extract the part of code that schedules jobs into a separate application and make sure only 1 instance is deployed so only 1 sets of jobs are scheduled? But then the problem becomes - what if this instance fails? How do I achieve fail over in this case?

Upvotes: 2

Views: 4176

Answers (1)

prem kumar
prem kumar

Reputation: 5877

You can make the task "scan a business related database table and schedule quartz jobs during starting up and every 10 minutes afterwards" itself a cron task in the clustered quartz instance.

Following is a spring based solution. Though you can use the idea to convert to non-spring based solution easily.

set up a cron trigger for every ten minutes.

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="runMeJob" />
    <property name="cronExpression" value="0 */10 * * * ?" />
</bean>

set up a Job. Here you will require a class instance in spring context.This class instance will have a public method which will invoked when trigger is fired. Within this method you will be writing the task to scan database and schedule these jobs.

<bean id="cronJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="yourObject" />
    <property name="targetMethod" value="methodOfThatObject" />     
</bean>

Configure trigger and job in the scheduler

<!--this should be your clustered quartz scheduler-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="jobDetails">
        <list>
            <ref bean="cronJob" />
        </list>
    </property>

    <property name="triggers">
        <list>
        <ref bean="cronTrigger" />
        </list>
    </property>
</bean>

Upvotes: 1

Related Questions