gpt4 book ai didi

java - Spring 3's Quartz 2 implementation doesn' t 不再支持 requestsRecovery

转载 作者:行者123 更新时间:2023-11-30 05:50:25 25 4
gpt4 key购买 nike

根据 Quartz 2 的 documentation

RequestsRecovery - if a job "requests recovery", and it is executing during the time of a 'hard shutdown' of the scheduler (i.e. the process it is running within crashes, or the machine is shut off), then it is re-executed when the scheduler is started again. In this case, the JobExecutionContext.isRecovering() method will return true.

Spring3 支持 Quartz 1.x 和 Quartz 2.x。如果我们使用 Quartz 1.x,那么我们需要使用以下配置来创建 JobDetail bean :

<bean id="ldapSynch" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.edfx.adb.scheduling.job.LDAPSynchronizer" />
<property name="requestsRecovery" value="true" />
</bean>

内部org.springframework.scheduling.quartz.JobDetailBean延伸org.quartz.JobDetail在 Quartz 1.x 中 org.quartz.JobDetail有一个二传手public void setRequestsRecovery(boolean shouldRecover) .

但如果我们使用 Spring3 的 Quartz 2.x 实现,则上述配置将更改为:

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

而且在 Quartz 2.x 中也没有这样的 org.quartz.JobDetail类,而是一个 interface最终org.springframework.scheduling.quartz.JobDetailFactoryBean不采用参数 <property name="requestsRecovery" value="true" />不再。

我们如何传递这个重要的parameter到 Quartz 的调度程序?

最佳答案

查看类的源代码后org.springframework.scheduling.quartz.JobDetailFactoryBean和 Quartz 2.0 的源代码我发现有一种 init methodorg.springframework.scheduling.quartz.JobDetailFactoryBean这是public void afterPropertiesSet() ;在此方法中,org.quartz.JobDetail 的实例正在创建。幸运的是 org.quartz.JobDetail 的这个实例可以通过方法访问 public JobDetail getObject()类(class) org.springframework.scheduling.quartz.JobDetailFactoryBean .

在 Quartz 2.0 中类 org.quartz.impl.JobDeialImpl实现接口(interface) org.quartz.JobDetail ;所以 org.quartz.JobDetail 的实例在 org.springframework.scheduling.quartz.JobDetailFactoryBean实际上是 org.quartz.impl.JobDeialImpl 的实例.

所以我创建了一个类com.edfx.adb.scheduling.ADBJobDetailFactoryBean延伸org.springframework.scheduling.quartz.JobDetailFactoryBean并覆盖 afterPropertiesSet()方法为:

package com.edfx.adb.scheduling;

import org.quartz.impl.JobDetailImpl;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;

public class ADBJobDetailFactoryBean extends JobDetailFactoryBean {

private boolean requestsRecovery;

public ADBJobDetailFactoryBean() {
super();
}

@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
JobDetailImpl jobDetail = (JobDetailImpl) getObject();
jobDetail.setRequestsRecovery(isRequestsRecovery());
}

public boolean isRequestsRecovery() {
return requestsRecovery;
}

public void setRequestsRecovery(boolean requestsRecovery) {
this.requestsRecovery = requestsRecovery;
}
}

并将 spring bean 配置更改为:

<bean id="ldapSynch" class="com.edfx.adb.scheduling.ADBJobDetailFactoryBean">
<property name="jobClass" value="com.edfx.adb.scheduling.job.LDAPSynchronizer" />
<property name="requestsRecovery" value="true" />
</bean>

瞧。我已经通过在调度程序执行任务时停止运行服务器以及当我重新启动服务器时调度程序开始执行未完成的作业来测试这一点。

希望对大家有帮助。

关于java - Spring 3's Quartz 2 implementation doesn' t 不再支持 requestsRecovery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14101741/

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