3

I have cron jobs and I want to add some condition to this jobs.

I mean before running quartz check this condition and if its true then execute job, if its not skip the job. Is there any way to do this?

Here is my schedulers;

<bean id="SystemServicePingExecutorJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="systemService" />
    <property name="targetMethod" value="executeSystemNodePing" />
</bean>
<bean id="SystemServiceLeaderExecutorJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject" ref="systemService" />
    <property name="targetMethod" value="executeLeaderResolution" />
</bean>


<bean id="systemServicePingCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="SystemServicePingExecutorJob" />
    <property name="cronExpression" value="0 0/15 * * * ?" />
</bean>
<bean id="systemServiceLeaderCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="SystemServiceLeaderExecutorJob" />
    <property name="cronExpression" value="0 0/30 * * * ?" />
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="overwriteExistingJobs" value="true"/>
    <property name="autoStartup" value="true" />
    <property name="quartzProperties">
        <props>
            <prop key="org.quartz.scheduler.instanceName">MyScheduler</prop>
            <prop key="org.quartz.scheduler.instanceId">AUTO</prop>
            <prop key="org.quartz.scheduler.skipUpdateCheck">true</prop>
            <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
            <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>
            <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
            <prop key="org.quartz.threadPool.threadCount">5</prop>                
        </props>
    </property>
    <property name="jobDetails">
        <list>
            <ref bean="SystemServicePingExecutorJob" />
            <ref bean="SystemServiceLeaderExecutorJob" />
        </list>
    </property>
    <property name="triggers">
        <list>
            <ref bean="systemServicePingCronTrigger" />                
            <ref bean="systemServiceLeaderCronTrigger" />                
        </list>
    </property>
</bean>
hellzone
  • 5,393
  • 25
  • 82
  • 148
  • how about telling to spring to not instantiating quartz bean at all if the condition isn't true. – O.Badr Feb 08 '17 at 14:13
  • Possible duplicate of [How to conditionally enable or disable scheduled jobs in Spring?](http://stackoverflow.com/questions/18406713/how-to-conditionally-enable-or-disable-scheduled-jobs-in-spring) -- The answer I was going to give you is very similar to the one accepted in that question: have your job's `execute()` method check the condition before any processing, and just return if the condition is not met. – walen Feb 09 '17 at 09:31

0 Answers0