Quartz错过的工作没有执行

时间:2012-12-29 14:25:23

标签: java quartz-scheduler spring-3

我在Spring中使用Quartz Scheduler。 Quartz的版本是2.1.6,Spring的版本是3.1.3。

以下是配置:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="autoStartup" value="true" />
    <property name="applicationContextSchedulerContextKey" value="applicationContext" />
    <property name="waitForJobsToCompleteOnShutdown" value="true" />
    <property name="overwriteExistingJobs" value="true" />
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="quartzProperties">
        <props>             
            <prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.StdJDBCDelegate</prop>
        </props>
    </property>

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

<bean id="ldapSynchJob" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="ldapSynch" />
    <property name="cronExpression" value="0 45 0 * * ?" />
    <property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_FIRE_ONCE_NOW" />
</bean>

<bean id="ldapSynch" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="com.edfx.adb.ldap.scheduler.LDAPSynchronizer" />
</bean>

我希望如果服务器停止并重新启动,并且如果调度程序错过了一个作业,那么一旦服务器启动它就会执行错过的作业。所以我使用了持久存储,数据库。但它似乎无法正常工作。

当我第一次启动我的服务器时,我在数据库中看到,在qrtz_triggers表中,列NEXT_FIRE_TIME的值为1356808500000,这意味着Sun Dec 30 00:45:00 IST 2012.执行调度程序后,它更改为1356894900000,即Sun Dec 31 00:45:00 IST 2012.然后我停止了我的服务器并更改了系统时间并将系统时间设置为12月31日12:47 AM。然后我再次启动我的服务器。我期待一旦服务器启动它就会在错过工作时执行调度程序。但是NEXT_FIRE_TIME值改为1356981300000而不是解雇作业,即IST 2013年1月1日00:45:00。

显然这不是我们想要的,我无法找到为什么会发生这种情况。任何建议都对我很有帮助。

我使用的SessionFactoryorg.springframework.orm.hibernate4.LocalSessionFactoryBeanhibernate.hbm2ddl.auto已设为update

更新

这是扩展QuartzJobBean

的类
public class LDAPSynchronizer extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<Attributes> currentUsersAttributes = LDAPManager.getManager().getCurrentUsersAttributes();
        List<Attributes> pastUsersAttributes = LDAPManager.getManager().getPastUsersAttributes();       
        getLDAPSynchService(context).synchronizeUser(currentUsersAttributes, pastUsersAttributes);
    }   

    private ILDAPSynchService getLDAPSynchService(JobExecutionContext context)  {
        ApplicationContext applicationContext = null;

        try {
            applicationContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext");
        } catch (Exception e) {         
        }

        if(applicationContext == null) {
            applicationContext = ContextLoader.getCurrentWebApplicationContext();
        }


        return (ILDAPSynchService) applicationContext.getBean("ldapSynchService");
    }
}

更新

每次服务器在控制台中启动时,我都会收到此日志:

01:28:24,586 INFO  [org.quartz.core.QuartzScheduler] (MSC service thread 1-10) Scheduler meta-data: Quartz Scheduler (v2.1.6) 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.springframework.scheduling.quartz.LocalDataSourceJobStore' - which supports persistence. and is not clustered.

01:28:24,596 INFO  [org.quartz.impl.StdSchedulerFactory] (MSC service thread 1-10) Quartz scheduler 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' initialized from an externally provided properties instance.
01:28:24,596 INFO  [org.quartz.impl.StdSchedulerFactory] (MSC service thread 1-10) Quartz scheduler version: 2.1.6
01:28:24,596 INFO  [org.quartz.core.QuartzScheduler] (MSC service thread 1-10) JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@1014d1b6
01:28:24,666 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT * FROM QRTZ_TRIGGERS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_NAME = ? AND TRIGGER_GROUP = ?

01:28:24,706 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT * FROM QRTZ_CRON_TRIGGERS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_NAME = ? AND TRIGGER_GROUP = ?

01:28:24,716 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT * FROM QRTZ_LOCKS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND LOCK_NAME = ? FOR UPDATE

01:28:24,716 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT JOB_NAME FROM QRTZ_JOB_DETAILS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND JOB_NAME = ? AND JOB_GROUP = ?

01:28:24,716 INFO  [stdout] (MSC service thread 1-10) Hibernate: UPDATE QRTZ_JOB_DETAILS SET DESCRIPTION = ?, JOB_CLASS_NAME = ?, IS_DURABLE = ?, IS_NONCONCURRENT = ?, IS_UPDATE_DATA = ?, REQUESTS_RECOVERY = ?, JOB_DATA = ?  WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND JOB_NAME = ? AND JOB_GROUP = ?

01:28:24,726 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT * FROM QRTZ_TRIGGERS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_NAME = ? AND TRIGGER_GROUP = ?

01:28:24,726 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT * FROM QRTZ_CRON_TRIGGERS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_NAME = ? AND TRIGGER_GROUP = ?

01:28:24,726 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT * FROM QRTZ_LOCKS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND LOCK_NAME = ? FOR UPDATE

01:28:24,736 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT J.JOB_NAME, J.JOB_GROUP, J.IS_DURABLE, J.JOB_CLASS_NAME, J.REQUESTS_RECOVERY FROM QRTZ_TRIGGERS T, QRTZ_JOB_DETAILS J WHERE T.SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND J.SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND T.TRIGGER_NAME = ? AND T.TRIGGER_GROUP = ? AND T.JOB_NAME = J.JOB_NAME AND T.JOB_GROUP = J.JOB_GROUP

01:28:24,736 INFO  [stdout] (MSC service thread 1-10) Hibernate: DELETE FROM QRTZ_SIMPLE_TRIGGERS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_NAME = ? AND TRIGGER_GROUP = ?

01:28:24,736 INFO  [stdout] (MSC service thread 1-10) Hibernate: DELETE FROM QRTZ_CRON_TRIGGERS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_NAME = ? AND TRIGGER_GROUP = ?

01:28:24,736 INFO  [stdout] (MSC service thread 1-10) Hibernate: DELETE FROM QRTZ_TRIGGERS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_NAME = ? AND TRIGGER_GROUP = ?

01:28:24,746 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT TRIGGER_NAME FROM QRTZ_TRIGGERS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_NAME = ? AND TRIGGER_GROUP = ?

01:28:24,746 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT TRIGGER_GROUP FROM QRTZ_PAUSED_TRIGGER_GRPS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_GROUP = ?

01:28:24,746 INFO  [stdout] (MSC service thread 1-10) Hibernate: SELECT TRIGGER_GROUP FROM QRTZ_PAUSED_TRIGGER_GRPS WHERE SCHED_NAME = 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' AND TRIGGER_GROUP = ?

01:28:24,746 INFO  [stdout] (MSC service thread 1-10) Hibernate: INSERT INTO QRTZ_TRIGGERS (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP, JOB_NAME, JOB_GROUP, DESCRIPTION, NEXT_FIRE_TIME, PREV_FIRE_TIME, TRIGGER_STATE, TRIGGER_TYPE, START_TIME, END_TIME, CALENDAR_NAME, MISFIRE_INSTR, JOB_DATA, PRIORITY)  VALUES('org.springframework.scheduling.quartz.SchedulerFactoryBean#0', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

01:28:24,756 INFO  [stdout] (MSC service thread 1-10) Hibernate: INSERT INTO QRTZ_CRON_TRIGGERS (SCHED_NAME, TRIGGER_NAME, TRIGGER_GROUP, CRON_EXPRESSION, TIME_ZONE_ID)  VALUES('org.springframework.scheduling.quartz.SchedulerFactoryBean#0', ?, ?, ?, ?)

之后我看到QUARTZ_TRIGGERS`表的PREV_FIRE_TIME设置为-1。并且行数永远不会超过1.我不知道它是否正常。

1 个答案:

答案 0 :(得分:1)

经过两天的学习和搜索网,最后我能够解决问题。这是正确的代码:

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="autoStartup" value="true" />
    <property name="applicationContextSchedulerContextKey" value="applicationContext" />
    <property name="waitForJobsToCompleteOnShutdown" value="true" />
    <property name="dataSource" ref="dataSource" />
    <property name="transactionManager" ref="transactionManager" />
    <property name="quartzProperties">
        <props>
            <prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.StdJDBCDelegate</prop>
        </props>
    </property>

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

<bean id="ldapSynchJob" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="ldapSynch" />
    <property name="misfireInstructionName" value="MISFIRE_INSTRUCTION_FIRE_ONCE_NOW" />
    <property name="cronExpression" value="0 17 12 * * ?" />
</bean>

<bean id="ldapSynch" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="com.edfx.adb.ldap.scheduler.LDAPSynchronizer" />       
</bean>

<property name="overwriteExistingJobs" value="true" />导致了这个问题。它覆盖了NEXT_FIRE_TIME表的QRTZ_TRIGGERS值。