spring定时器

在项目中我需要一个定时器,在每天访问量很低,或者没有人访问的时候,把需要统计的数据进行采集,分析,存储,以便于高层管理者决策,参考.

所以既然我的项目利用了spring.为什么不用spring封装好的定时器呢. 好简单的o.



step1:新建ReportTask.java类. 该类继承了java.util.TimerTask,并且需要复写TimerTask类中的run方法.



  
package com.tiros.caredog.cd.util;   
  
import java.sql.Connection;   
import java.sql.PreparedStatement;   
import java.util.TimerTask;   
  
import org.apache.log4j.Logger;   
import org.springframework.context.ApplicationContext;   
import org.springframework.context.support.ClassPathXmlApplicationContext;   
  
import com.tiros.caredog.cd.dao.ReportDao;   
import com.tiros.caredog.cd.pojo.Report;   
  
public class ReportTask extends TimerTask {   
  
    private Logger logger = Logger.getLogger(ReportTask.class);   
  
    public void run() {   
        ApplicationContext context = new ClassPathXmlApplicationContext(   
                "applicationContext.xml");   
        ReportDao reportDao = (ReportDao) context.getBean("reportDaoImp");   
        DateUtil dateUtil = new DateUtil();   
  
        for (int j = 1; j <= 3; j++) {// 日期   
            String day = dateUtil.convert(dateUtil.theDayBeforeSomeDay(j),"yyyy-MM-dd");// 昨天 /前天/ 大前天   
            for (int i = 1; i <= 2; i++) {// 版本号   
                boolean isBackup = reportDao.checkIsBackup(day, i);   
                if (!isBackup) {// 没有备份   
                    Report report = reportDao.queryTheDayBeforeSomeDay(day, i);   
                    saveReport(report);   
                }   
            }   
        }   
    }   
  
       
}  

package com.tiros.caredog.cd.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tiros.caredog.cd.dao.ReportDao;
import com.tiros.caredog.cd.pojo.Report;

public class ReportTask extends TimerTask {

	private Logger logger = Logger.getLogger(ReportTask.class);

	public void run() {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		ReportDao reportDao = (ReportDao) context.getBean("reportDaoImp");
		DateUtil dateUtil = new DateUtil();

		for (int j = 1; j <= 3; j++) {// 日期
			String day = dateUtil.convert(dateUtil.theDayBeforeSomeDay(j),"yyyy-MM-dd");// 昨天 /前天/ 大前天
			for (int i = 1; i <= 2; i++) {// 版本号
				boolean isBackup = reportDao.checkIsBackup(day, i);
				if (!isBackup) {// 没有备份
					Report report = reportDao.queryTheDayBeforeSomeDay(day, i);
					saveReport(report);
				}
			}
		}
	}

	
}


上面的流程看懂看不懂都无所谓, 反正都是到达规定时间后,自动执行的.

关键点是extends TimerTask. 并且复写run()方法.

然后再applicationContext.xml中配置如下

<!--spring 定时器 -->   
<bean id="quartzClock"  
    class="org.springframework.scheduling.quartz.JobDetailBean">   
    <property name="jobClass">   
        <value>com.tiros.caredog.cd.util.ReportTask</value>   
    </property>   
</bean>   
  
<bean id="cronQuartzClock"  
    class="org.springframework.scheduling.quartz.CronTriggerBean">   
    <property name="jobDetail" ref="quartzClock"></property>   
    <property name="cronExpression">   
        <value>0 1,2,3,4,5,6,7,8,9,10 1 * * ?</value>   
        <!-- 每天凌晨1点1分到1点10分自动备份 -->   
        <!--   秒  份  时 日 月 星期 年  -->   
        <!-- 区间   *通配/每天/月/年  ?   
         -->   
    </property>   
</bean>   
  
<bean   
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">   
    <property name="triggers">   
        <list>   
            <ref bean="cronQuartzClock" />   
        </list>   
    </property>   
</bean>  

	<!--spring 定时器 -->
	<bean id="quartzClock"
		class="org.springframework.scheduling.quartz.JobDetailBean">
		<property name="jobClass">
			<value>com.tiros.caredog.cd.util.ReportTask</value>
		</property>
	</bean>

	<bean id="cronQuartzClock"
		class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="quartzClock"></property>
		<property name="cronExpression">
			<value>0 1,2,3,4,5,6,7,8,9,10 1 * * ?</value>
			<!-- 每天凌晨1点1分到1点10分自动备份 -->
			<!--   秒  份  时 日 月 星期 年  -->
			<!-- 区间   *通配/每天/月/年  ?
			 -->
		</property>
	</bean>
	
	<bean
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="cronQuartzClock" />
			</list>
		</property>
	</bean> 



这样,一个spring的timertask就ok了.
转载:http://songzj.iteye.com/blog/811605

你可能感兴趣的:(spring)