spring 使用动态定时器quartz
需添加jar:quartz-all-1.8.5.jar
spring中的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!--spring集成quartz的定时器 --> <!-- 线程执行器配置,用于任务注册 --> <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="10" /> <property name="maxPoolSize" value="100" /> <property name="queueCapacity" value="500" /> </bean> <!-- 业务对象 --> <bean id="autoAudit" class="com.easipass.business.auto.AutoAudit" > <property name="scheduler" ref="schedulerFactory" /> </bean> <!-- 调度业务 --> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="autoAudit" /> <property name="targetMethod" value="doAutoAudit" /> </bean> <!--增加调度触发器 --> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="jobDetail" /> <property name="cronExpression" value="0 27 10 * * ?" /> </bean> <!-- 设置调度 --> <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>
调用方法:
1 package com.easipass.business.auto; 2 3 4 import javax.annotation.Resource; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.quartz.Scheduler; 9 import org.springframework.scheduling.quartz.CronTriggerBean; 10 11 import com.easipass.business.service.interfaces.IAutoAuditService; 12 import com.easipass.business.util.SysOptionsUtil; 13 14 public class AutoAudit { 15 16 @Resource(name="autoAuditService") 17 private IAutoAuditService autoAuditService ; 18 19 private Log logger = LogFactory.getLog(getClass()); 20 21 private Scheduler scheduler; 22 23 public void setScheduler(Scheduler scheduler) { 24 this.scheduler = scheduler; 25 } 26 27 28 public void doAutoAudit() 29 { 30 try{ 31 CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger("cronTrigger", Scheduler.DEFAULT_GROUP); 32 String dbCronExpression = SysOptionsUtil.getFinanceAuditTime(); 33 String originConExpression = trigger.getCronExpression(); 34 // 如果数据库中的定时器表达式和现有的一致,则不需要重新rescheduleJob 35 if(!originConExpression.equalsIgnoreCase(dbCronExpression)){ 36 trigger.setCronExpression(dbCronExpression); 37 scheduler.rescheduleJob("cronTrigger", Scheduler.DEFAULT_GROUP, trigger); 38 } 39 autoAuditService.autoAudit(); 40 41 }catch(Exception e){ 42 logger.error("自动审核出错:",e); 43 } 44 } 45 46 }
32行的SysOptionsUtil.getFinanceAuditTime()表示从数据库中读取的定时表达式。这样就可以从数据库动态读取了,如果定时需求发生变化就无需更改代码,只要成功起一下web服务即可。