<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1. 需要的lib: 前两个是spring必备的包,后三个是定时任务需要的包</span>
spring.jar
spring-sources.jar
commons-collections-3.1.jar
jta.jar
quartz-all-1.6.0.jar
2. applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="timingTask" class="com.ctrip.bi.uss.service.TimingTask" />
<bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="timingTask" />
</property>
<property name="targetMethod">
<value>LoadData</value>
</property>
</bean>
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="methodInvokingJobDetail" />
</property>
<span style="white-space:pre"> </span><!-- 每隔1分钟调度一次 -->
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="cronTrigger" />
</list>
</property>
</bean>
</beans>
3. 定时任务类TimingTask
<pre name="code" class="plain">package com.ctrip.bi.uss.service;
import org.apache.log4j.Logger;
import com.ctrip.bi.uss.dao.LoadDataDao;
public class TimingTask {
public static Logger log = Logger.getLogger(TimingTask.class);
CheckFileVersion cfv = new CheckFileVersion();
long cVersion = 1407310670846L;
long localVersion = 0;
String path = "D:\\Data";
public void LoadData() {
LoadDataDao loadDataImpl = new LoadDataDao();
try {
log.info("Start task>........");
// 查看目录下文件的版本
localVersion = cfv.GetFileVersion(path);
if (cVersion < localVersion) {
// 定时导入数据
loadDataImpl.loadToBdb(false);
} else {
log.info("Current version is the newest one. No need to update!");
}
System.out.println("Time:" + new java.util.Date().toLocaleString()
+ "]----->Loading value...");
log.info("End task!");
} catch (Exception e) {
log.error( e.getCause()+e.getMessage());
}
}
}
4. 测试的类 TestTimingTask
package com.ctrip.bi.uss.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestTimingTask {
public static void main(String[] args) {
System.out.println("Loading spring configuaration file....");
ApplicationContext context = new ClassPathXmlApplicationContext(
"/applicationContext.xml");
System.out.println("Finish loading file!");
}
}