Spring Quartz定时详情
第四步,配置一个调度器。在Spring配置文件中配置调度器类SchedulerFactoryBean。
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
附:cronExpression表达式解释:
字段 允许值 允许的特殊字符
秒 0-59 , - * /
分 0-59 , - * /
小时 0-23 , - * /
日期 1-31 , - * ? / L W C
月份 1-12 或者 JAN-DEC , - * /
星期 1-7 或者 SUN-SAT , - * ? / L C #
年(可选) 留空, 1970-2099 , - * /
0 0 12 * * ?---------------在每天中午12:00触发
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:00至2:59之间每分钟触发一次
0 0/5 14 * * ?---------------每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18 * * ?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 * * ?---------------每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14 ? 3 WED---------------每三月份的星期三在下午2:00和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, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6#3---------------在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 * ?---------------从每月的第一天起每过5天的中午12:00时触发
0 11 11 11 11 ?---------------在每个11月11日的上午11:11时触发.
参考资料:
1.http://www.springframework.org Spring的官方网站
2.http://www.opensymphony.com/quartz/ Quartz的官方网站
程序逻辑:web.xml添加
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="remindQuartz" class="test.quartz.RemindQuartz"/>
<bean id="remindQuartzTask" class= "org.springframework.scheduling.quartz. ">
<property name="targetObject" ref="remindQuartz" />
<property name="targetMethod" value="perform" />
<property name="concurrent" value="false" />
</bean>
<!-- 调度器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="remindQuartzTask" />
<property name="cronExpression" value="0 0 1 1 * ?" />
</bean>
</beans>
附:实际代码*
xml:
<?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:util="http://www.springframework.org/schema/util"
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.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
">
<!-- quartz scheduler实例在spring中的初始化和自动运行 -->
<bean id="timerJob" class="com.eduwo.application.web.evaluationSheets.TimerJob">
<property name="evaluationSheetService">
<ref bean="evaluationSheetService"/>
</property>
</bean>
<bean id="methodInvokingJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject"><ref bean="timerJob"/></property>
<property name="targetMethod"><value>doJob</value></property>
<property name="concurrent" value="false"/>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="methodInvokingJobDetail"/>
</property>
<property name="cronExpression" value="0 0/60 * ? * * *" />
</bean>
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">
<property name="configLocation" value="classpath:quartz.properties" />
<property name="triggers">
<list>
<ref local="simpleTrigger"/>
</list>
</property>
</bean>
</beans>
JAVA类:
package com.eduwo.application.web.evaluationSheets;
import java.util.Date;
import org.apache.log4j.Logger;
import com.eduwo.application.service.evaluationSheets.EvaluationSheetService;
public class TimerJob {
Logger logger=Logger.getLogger(TimerJob.class);
private EvaluationSheetService evaluationSheetService;
public EvaluationSheetService getEvaluationSheetService() {
return evaluationSheetService;
}
public void setEvaluationSheetService(
EvaluationSheetService evaluationSheetService) {
this.evaluationSheetService = evaluationSheetService;
}
public void doJob(){
Date d =new Date();
logger.info("Quartz begin ------------------------------"+d.toLocaleString()+"-----"+d.getHours());
//评估单过期处理
evaluationSheetService.updateExpiredEvaluationSheet();
logger.info("Quartz end ------------------------------");
}
}
public void updateExpiredEvaluationSheet() {
// TODO Auto-generated method stub
Date date=new Date(System.currentTimeMillis()-2*24*60*60*1000);
SimpleDateFormat formattxt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timejob=formattxt.format(date);
String hql="select count(*) from ComTevaluationsheet sheet where sheet.updatedate is not null"
+" and sheet.expired='0' and sheet.status in ('1','2') and sheet.updatedate<=to_date('" + timejob + "'," + "'"
+ "yyyy-mm-dd hh24:mi:ss')";
List list=new ArrayList();
list=this.get(hql);
int count=0;
if(list!=null&&list.size()>0) count=(Integer)list.get(0);
if(count>0){
System.out.println("start---");
hql="select sheet from ComTevaluationsheet sheet where sheet.updatedate is not null"
+" and sheet.expired='0' and sheet.updatedate<=to_date('" + timejob + "'," + "'"
+ "yyyy-mm-dd hh24:mi:ss')";
list = this.get(hql);
for (int i = 0; i < list.size(); i++) {
ComTevaluationsheet comTevaluationsheet=(ComTevaluationsheet) list.get(i);
comTevaluationsheet.setExpired("1");
comTevaluationsheet.setStatus("3");
//this.updateComTevaluationsheet(comTevaluationsheet);
}
}
}