spring 定时任务

<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!");
		
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值