Quartz和spring 集群定时任务整合 下载本文

}

public void setApplicationContext(ApplicationContext applicationContext) {

this.ctx = applicationContext; }

public String getTargetObject() { return targetObject; }

public void setTargetObject(String targetObject) { this.targetObject = targetObject; }

public String getTargetMethod() { return targetMethod; }

public void setTargetMethod(String targetMethod) { this.targetMethod = targetMethod; } }

5. 再来看完整的 quartz.xml (注意红色加粗部分尤为重要): applicationContext-quartz.xml

\

class=\ryBean\

0 0/1 * * * ? //一分钟执行一次

6. 在web.xml 中导入

contextConfigLocation

/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-quartz.xml

7. 结尾

几个节点都带有 quartz 任务,此时只有一台 quartz 在运行,另几个节点上的 quartz 没有运行。

此时手动 shutdown 那台运行 QUARTZ (在程序里

加 system.out.println(“execute once…”), 运行 quartz 的那个节点在后台会打印 execute once )的节点,过了 7 秒左右,另一个节点的 quartz 自动监测到了集群中运行着的 quartz 的 instance 已

经 shutdown ,因此 quartz 集群会自动把任一台可用的APP 上启动起一个 quartz job 的任务。

自此, QUARTZ 使用 HA 策略的集群大功告成,不用改原有代码,配置一下我们就可作到 QUARTZ 的集群与自动错误冗余。