设为首页 加入收藏

TOP

Spring 中配置定时调度两种方法介绍
2014-11-23 17:59:59 】 浏览:323
Tags:Spring 配置 定时 调度 方法 介绍

Spring 中配置定时调度两种方法介绍


方法一:


直接用jdk api的Timer类,无需配置spring文件


1、用@compent注解,实现InitializingBean接口 ,spirng会自动去查找afterPropertiesSet()方法,


2、在afterPropertiesSet方法中写业务实现,调用timer的schedule方法或者scheduleAtFixedRate方法


schedule(TimerTask task, Date time)
安排在指定的时间执行指定的任务。


scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
安排指定的任务在指定的时间开始进行重复的固定速率执行。


代码实现如下:


import java.util.Calendar;
import java.util.Date;
import java.util.Timer;


import java.util.TimerTask;
import javax.annotation.Resource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import com.cssweb.payment.account.service.support.AccountServiceSupport;


@Component
public class GetTimer implements InitializingBean{

private Date date;

@Override
public void afterPropertiesSet() throws Exception {
init();//初始化参数,每天凌晨00:00:00开始作业
//System.out.println("时间是:============="+date);
new Timer().schedule(test(), date); //test()为自己要处理的业务实现方法
}

/**
* 初始化参数
*
*/
public void init(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.add(Calendar.DAY_OF_MONTH, 1);
date = cal.getTime();
}



public static TimerTask test(){
return new TimerTask() {
@Override
public void run() {
System.out.println(new Date()+"开始了------------------------------");
}
};
}



}


方法二:用spring配置文件进行配置


< xml version="1.0" encoding="UTF-8" >
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">

//找到对应的类名

















】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java任务调度框架Quartz教程实例 下一篇Spring配置Quartz任务调度框架教程

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目