Spring Quartz定时器详情
Spring Quartz定时详情
第四步,配置一个调度器。在Spring配置文件中配置调度器类SchedulerFactoryBean。
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 我们的任务 --> <bean name=" reportTask " class= "org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value=" BusinessReport " /> </bean> <!-- 触发器 --> <bean id="cronTrigger" class= "org.springframework.scheduling.quartz.CronTriggerBean"> <!-- 指向我们的任务 --> <property name="jobDetail" ref=" reportTask " /> <!-- 每月1日凌晨1点运行 --> <property name="cronExpression" value="0 0 1 1 * ?" /> </bean> <!-- 调度器 --> <bean class= "org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list><!-- 触发器列表--> <ref bean="cronTrigger" /> </list> </property> </bean> </beans>
附:cronExpression表达式解释:
字段 允许值 允许的特殊字符
秒0-59,-*/
分0-59,-*/
小时0-23,-*/
日期1-31,-*?/LWC
月份1-12或者JAN-DEC,-*/
星期1-7或者SUN-SAT,-*?/LC#
年(可选) 留空, 1970-2099 , - * /0012**?---------------在每天中午12:00触发
01510?**---------------每天上午10:15触发
01510**?---------------每天上午10:15触发
01510**?*---------------每天上午10:15触发
01510**?2005---------------在2005年中的每天上午10:15触发
0*14**?---------------每天在下午2:00至2:59之间每分钟触发一次
00/514**?---------------每天在下午2:00至2:59之间每5分钟触发一次
00/514,18**?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
00-514**?---------------每天在下午2:00至2:05之间每分钟触发一次
010,4414?3WED---------------每三月份的星期三在下午2:00和2:44时触发
01510?*MON-FRI---------------从星期一至星期五的每天上午10:15触发
0151015*?---------------在每个月的每15天的上午10:15触发
01510L*?---------------在每个月的最后一天的上午10:15触发
01510?*6L---------------在每个月的最后一个星期五的上午10:15触发
01510?*6L2002-2005---------------在2002,2003,2004and2005年的每个月的最后一个星期五的上午10:15触发
01510?*6#3---------------在每个月的第三个星期五的上午10:15触发
00121/5*?---------------从每月的第一天起每过5天的中午12:00时触发
0 11 11 11 11 ?---------------在每个11月11日的上午11:11时触发.参考资料:
1.http://www.springframework.orgSpring的官方网站
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.MethodInvokingJobDetailFactoryBean"> <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); } } }