歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring 中配置定時調度兩種方法介紹

Spring 中配置定時調度兩種方法介紹

日期:2017/3/1 9:37:56   编辑:Linux編程

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"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">

<bean id="remindSchedule" class="com.checking_in_remind.controller.Remind" /> //找到對應的類名

<!-- 定義調用對象和調用對象的方法(jobDetail) -->
<bean id="remindtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 注冊定時任務執行時調用的類 -->
<property name="targetObject" ref="remindSchedule" />
<!-- 注冊定時任務執行時調用的方法 -->
<property name="targetMethod" value="getPersonInfo" />
<!-- 此參數為false等於JobDetail對象實現了Stateful接口,jobs不會並發運行-->
<property name="concurrent" value="false" />
</bean>

<bean id="remindCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="remindtask" />
<!-- 每5分鐘執行一次任務調度 -->
<property name="cronExpression" value="0 */5 * * * ?"/>
</bean>
</beans>

Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm

運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved