Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two schedulers in one Spring context

I created a Spring application with two quartz schedulers with the first having 3 triggers and second having one trigger.

All those triggers are set to concurrent operation false.

When I run this, it seems like only the trigger of the job in the second scheduler get fired. The triggerJobB takes very little time to execute too.

My question is that does using two schedulers in one spring context cause executing problems and whether it's a good practice.

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                  <ref bean="triggerJobA" />
                  <ref bean="triggerJobC" />
                  <ref bean="triggerJobD" />
            </list>
        </property>
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="triggerJobB" />
            </list>
        </property>
</bean>
like image 417
Prasad Weera Avatar asked Dec 21 '12 07:12

Prasad Weera


1 Answers

You need to set the name for each scheduler, otherwise spring just overwrites the first scheduler with the second one.

 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="schedulerName" value="SchedulerOne"/> 
    <property name="triggers">
        <list>
              <ref bean="triggerJobA" />
              <ref bean="triggerJobC" />
              <ref bean="triggerJobD" />
        </list>
    </property>
 </bean>


 <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
     <property name="schedulerName" value="SchedulerTwo"/>
     <property name="triggers">
        <list>
            <ref bean="triggerJobB" />
        </list>
    </property>
 </bean>

Reason for this is:

The "job executor" is actually not the SchedulerFactoryBean. It is the Scheduler bean(to be precise calling its start method invokes the aggregated QuartzScheduler.start method which fires the Trigger-s), provided by the SchedulerFactoryBean. As a matter of fact this Scheduler is stored(and looked-up) under the schedulerName(which if not explicitly set has the same default value for every configured SchedulerFactoryBean) in the SchedulerRepository singleton(SchedulerRepository.getInstance()).

That's how unless you set a different schedulerName for your SchedulerFactoryBean-s, you will always get the same scheduler by each and every SchedulerFactoryBean-s

http://forum.springsource.org/showthread.php?40945-Multiple-Quartz-SchedulerFactoryBean-instances

like image 131
drobson Avatar answered Sep 23 '22 15:09

drobson