gpt4 book ai didi

java - quartz 错过的工作没有执行

转载 作者:行者123 更新时间:2023-11-30 11:37:22 24 4
gpt4 key购买 nike

我在 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:47AM。然后我再次启动我的服务器。我期望调度程序会在服务器启动后立即执行,因为它错过了一项工作。但是 NEXT_FIRE_TIME 值没有触发作业,而是更改为 1356981300000,即 2013 年 1 月 1 日星期二 00:45:00 IST。

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

我正在使用的 SessionFactoryorg.springframework.orm.hibernate4.LocalSessionFactoryBean 并且 hibernate.hbm2ddl.auto 已经设置更新

更新

这是扩展 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,不知道是不是正常。

最佳答案

经过两天的研究和搜索,终于解决了这个问题。这是正确的代码:

<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 的值表。

关于java - quartz 错过的工作没有执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14082563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com