2

我正在使用带有 Spring 的 Quartz 调度程序。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,这意味着 IST 2012 年 12 月 30 日 00:45:00 。调度程序执行后,它更改为 1356894900000,即 12 月 31 日 00 日: 2012 年 IST 45:00。然后我停止了服务器并更改了系统时间并将系统时间设置为 12 月 31 日上午 12:47。然后我再次启动我的服务器。我期待调度程序会在服务器启动后立即执行,因为它错过了一项工作。但是 NEXT_FIRE_TIME 值没有触发作业,而是更改为 1356981300000,即 Tue Jan 01 00:45:00 IST 2013。

显然这是不希望的,我无法找到发生这种情况的原因。任何建议都会对我很有帮助。

SessionFactory我使用的是并且org.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', ?, ?, ?, ?)

之后我看到PREV_FIRE_TIMEQUARTZ_TRIGGERS 表设置为 -1。并且行数永远不会超过1。我不知道这是否正常。

4

1 回答 1

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_TIMEQRTZ_TRIGGERS

于 2012-12-31T07:16:37.343 回答