Quartz在Spring中动态设置cronExpression (spring设置动态定时任务)
转载 http://hi.baidu.com/vip099/blog/item/51bbb03d555f5702bba16766.html
什么是动态定时任务:是由客户制定生成的,服务端只知道该去执行什么任务,但任务的定时是不确定的(是由客户制定)。这样总不能修改配置文件每定制个定时任务就增加一个trigger吧,即便允许客户修改配置文件,但总需要重新启动web服务啊,研究了下Quartz在Spring中的动态定时,发现:
Xml代码
<bean id="cronTrigger" >
<property name="jobDetail" ref="schedulerJobDetail"/>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
<bean id="cronTrigger" >
<property name="jobDetail" ref="schedulerJobDetail"/>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
中cronExpression是关键,如果可以动态设置cronExpression的值,也就说如果我们可以直接调用CronTriggerBean中设置cronExpression的方法,就可以顺利解决问题了。
熟悉1的朋友可以跳过不看,下面2、3是动态定时任务的具体实现。1. Quartz在Spring中的简单配置Spring配置文件:
Xml代码
<bean id="schedulerJobDetail" ref="scheduleInfoAction"/>
<property name="targetMethod" value="simpleJobTest"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" >
<property name="jobDetail" ref="schedulerJobDetail"/>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<bean id="schedulerFactory" ref="scheduleInfoAction"/>
<property name="targetMethod" value="simpleJobTest"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" >
<property name="jobDetail" ref="schedulerJobDetail"/>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<bean id="schedulerFactory" + "' Success..."); }2.Quartz在Spring中动态设置cronTrigger方法一Spring配置文件:
Xml代码
<bean id="scheduleInfoAction" ref="schedulerFactory"/>
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerJobDetail" ref="scheduleInfoAction"/>
<property name="targetMethod" value="reScheduleJob"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" >
<property name="jobDetail" ref="schedulerJobDetail"/>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<bean id="schedulerFactory" ref="schedulerFactory"/>
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerJobDetail" ref="scheduleInfoAction"/>
<property name="targetMethod" value="reScheduleJob"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" >
<property name="jobDetail" ref="schedulerJobDetail"/>
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<bean id="schedulerFactory" and scheduleInfo.infoId = '"+"1" + "'";
List scheduleList = scheduleInfoManager.queryScheduleInListBySql(sql);
ScheduleInfo scheduleInfo = (ScheduleInfo)scheduleList.get(0);
String dbCronExpression = scheduleInfo.getCronExpression();
return dbCronExpression;
}
private String getCronExpressionFromDB(){
String sql="from ScheduleInfo scheduleInfo where 1=1 ";
sql=sql+" and scheduleInfo.infoId = '"+"1" + "'";
List scheduleList = scheduleInfoManager.queryScheduleInListBySql(sql);
ScheduleInfo scheduleInfo = (ScheduleInfo)scheduleList.get(0);
String dbCronExpression = scheduleInfo.getCronExpression();
return dbCronExpression;
}
③ 在spring配置文件的scheduleInfoAction配置了相应的property(scheduler/ scheduleInfoManager),要为其设置setter方法:
Java代码
private Scheduler scheduler;
// 设值注入,通过setter方法传入被调用者的实例scheduler
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
private ScheduleInfoManager scheduleInfoManager;
// 设值注入,通过setter方法传入被调用者的实例scheduleInfoManager
public void setScheduleInfoManager(ScheduleInfoManager scheduleInfoManager){
this.scheduleInfoManager = scheduleInfoManager;
}
private Scheduler scheduler;
// 设值注入,通过setter方法传入被调用者的实例scheduler
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
private ScheduleInfoManager scheduleInfoManager;
// 设值注入,通过setter方法传入被调用者的实例scheduleInfoManager
public void setScheduleInfoManager(ScheduleInfoManager scheduleInfoManager){
this.scheduleInfoManager = scheduleInfoManager;
}
Quartz在Spring中动态设置cronTrigger方法二在上面的2中我们可以看到,尽管已经可以动态进行rescheduleJob了,不过依然需要我们设置一个cronExpression,如果尝试一下拿掉spring配置中的
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
则容器(如tomcat)启动时会报错。实际中我们希望tomcat启动时就可以直接去读数据库,拿到相应的dbCronExpression,然后定时执行一个job,而不希望配置初始的cronExpression ,观察下面的CronTriggerBean,考虑到cronExpression需要初始化,如果设定一个类InitializingCronTrigger继承CronTriggerBean,然后在这个类中做一些读取DB的初始化工作(设置cronExpression),问题就可以解决了。Spring配置文件:
Xml代码
<bean id="scheduleInfoAction" ref="schedulerFactory"/>
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerJobDetail" ref="scheduleInfoAction"/>
<property name="targetMethod" value="reScheduleJob"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" ref="schedulerJobDetail"/>
<!--<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>-->
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerFactory" ref="schedulerFactory"/>
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerJobDetail" ref="scheduleInfoAction"/>
<property name="targetMethod" value="reScheduleJob"/>
<property name="concurrent" value="false"/>
</bean>
<bean id="cronTrigger" ref="schedulerJobDetail"/>
<!--<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>-->
<property name="scheduleInfoManager" ref="scheduleInfoManager"/>
</bean>
<bean id="schedulerFactory" and scheduleInfo.infoId = '"+"1" + "'";
List scheduleList = scheduleInfoManager.queryScheduleInListBySql(sql);
ScheduleInfo scheduleInfo = (ScheduleInfo)scheduleList.get(0);
String dbCronExpression = scheduleInfo.getCronExpression();
return dbCronExpression;}……}
public class InitializingCronTrigger extends CronTriggerBean implements Serializable {
private ScheduleInfoManager scheduleInfoManager;
// 设值注入,通过setter方法传入被调用者的实例scheduleInfoManager
public void setScheduleInfoManager(ScheduleInfoManager scheduleInfoManager){
this.scheduleInfoManager = scheduleInfoManager;
// 因为在getCronExpressionFromDB使用到了scheduleInfoManager,所以
// 必须上一行代码设置scheduleInfoManager后进行
getCronExpressionFromDB
String cronExpression = getCronExpressionFromDB ();
// ①
// 因为extends CronTriggerBean ,此处调用父类方法初始化cronExpression
setCronExpression(cronExpression);
// ②}
private String getCronExpressionFromDB(){
String sql="from ScheduleInfo scheduleInfo where
1=1 ";
sql=sql+" and scheduleInfo.infoId = '"+"1" + "'";
List scheduleList = scheduleInfoManager.queryScheduleInListBySql(sql);
ScheduleInfo scheduleInfo = (ScheduleInfo)scheduleList.get(0);
String dbCronExpression = scheduleInfo.getCronExpression();
return dbCronExpression;}……}
附表:"0 0 12 * * ?" 每天中午12点触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
至于每个符号 看看例子就好了.很简单了.