javacronquartz-scheduler

Quartzscheduler run on 1st and 3rd Monday of the month


I'd like to run a job every first and third Monday of the month. I'm using a CronTriggerBean that I'm trying to configure with the following expressions but i doesn't work:

<property name="cronExpression" value="0 0 12 ? * MON#1,3 *" />

or

<property name="cronExpression" value="0 0 12 ? * MON#1,MON#3 *" />

The first expression only runs the job on the first Monday while the second one runs the job on the third Monday.

Is there any way I could achieve this with a CronTriggerBean? I'm using quartz-1.6.5 with XML config so I don't think I could configure a SimpleTriggerBean to do it.


Solution

  • You won't be able to do that with a single trigger bean. You will need to create 2 separate and register them with the scheduler:

    <bean id="cronTriggerJobFirstMonday"
                class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="YourQuartzJobBean" />
        <property name="cronExpression" value="0 0 12 ? * MON#1 *" />
    </bean>
    
    <bean id="cronTriggerJobThirdMonday"
                class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="yourQuartzJobBean" />
        <property name="cronExpression" value="0 0 12 ? * MON#3 *" />
    </bean>
    
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTriggerJobFirstMonday" />
                <ref bean="cronTriggerJobThirdMonday" />
            </list>
        </property>
    </bean>