Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running A Missed Quartz Job

I'm using Quartz with Spring to run a specific task at midnight on the first day of the month. I have tested the job by setting my server date & time to be 11:59 on the last day of the month, starting the server and observing the task run when it turns to 12:00, but I'm concerned about cases where the server (for whatever reason) may not be running at midnight on the first of the month.

I'd assumed that misfire handling in Quartz would care for this, but maybe I'm mistaken on that?

Can anyone advise me on how I might be able to handle this? I'd really prefer not to create a job that runs every 'x' seconds/minutes/hours and check to see if I need to run the job if I can avoid it.

I'm also curious as to why I'm not seeing any Quartz related logging info, but that's a secondary issue.

Here is my spring configuration for the task:

<bean id="schedulerService" class="com.bah.pams.service.scheduler.SchedulerService">
    <property name="surveyResponseDao" ref="surveyResponseDao"/>
    <property name="organizationDao" ref="organizationDao"/>
</bean>

<bean name="createSurveyResponsesJob" class="org.springframework.scheduling.quartz.JobDetailBean">
    <property name="jobClass" value="com.bah.pams.service.scheduler.jobs.CreateSurveyResponsesJob"/>
    <property name="jobDataAsMap">
        <map>
            <entry key="schedulerService" value-ref="schedulerService"/>
        </map>
    </property>
</bean>
<!-- Cron Trigger -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="createSurveyResponsesJob"/>
    <property name="cronExpression" value="0 0 0 1 * ? *"/>
    <!--if the server is down at midnight on 1st of month, run this job as soon as it starts up next -->
    <property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_FIRE_ONCE_NOW"/>
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

    <property name="autoStartup" value="true"/>

    <property name="quartzProperties">
        <props>
            <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>
            <prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
        </props>
    </property>
    <property name="jobDetails">
        <list>
            <ref bean="createSurveyResponsesJob"/>
        </list>
    </property>

    <property name="triggers">
        <list>
            <ref bean="cronTrigger"/>
        </list>
    </property>
</bean>
like image 895
Todd Sharp Avatar asked Nov 04 '22 10:11

Todd Sharp


1 Answers

MISFIRE_INSTRUCTION_FIRE_ONCE_NOW is for the purpose you mentioned and if you suspect a shutdown of the server(s) you should definitely persist your jobs out of the JVM memory (ex: by using a JDBCJobStore instead of a RAMJobStore).

RAMJobStore is fast and lightweight, but all scheduling information is lost when the process terminates.

http://quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigRAMJobStore

JDBCJobStore is used to store scheduling information (job, triggers and calendars) within a relational database.

http://quartz-scheduler.org/documentation/quartz-2.x/configuration/ConfigJobStoreTX

Hope it help.

like image 84
ebtokyo Avatar answered Nov 09 '22 10:11

ebtokyo