在Spring3中使用注解(@Scheduled)創建計划任務


Spring3中加強了注解的使用,其中計划任務也得到了增強,現在創建一個計划任務只需要兩步就完成了:

  1. 創建一個Java類,添加一個無參無返回值的方法,在方法上用@Scheduled注解修飾一下;
  2. 在Spring配置文件中添加三個<task:**** />節點;

最后說明一下,第一步創建的Java類要成為Spring可管理的Bean,可以直接寫在XML里,也可以@Component一下

計划任務類:

/**
 * com.zywang.spring.task.SpringTaskDemo.java
 * @author ZYWANG 2011-3-9
 */
package com.zywang.spring.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Spring3 @Scheduled 演示
 * @author ZYWANG 2011-3-9Spring配置文件:
 */
@Component
public class SpringTaskDemo {

    @Scheduled(fixedDelay = 5000)
    void doSomethingWithDelay(){
        System.out.println("I'm doing with delay now!");
    }
    
    @Scheduled(fixedRate = 5000)
    void doSomethingWithRate(){
        System.out.println("I'm doing with rate now!");
    }
    
    @Scheduled(cron = "0/5 * * * * *")
    void doSomethingWith(){
        System.out.println("I'm doing with cron now!");
    }
}

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:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    <!-- Enables the Spring Task @Scheduled programming model -->
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>

需要注意的幾點:

1、spring的@Scheduled注解  需要寫在實現上、

2、 定時器的任務方法不能有返回值(如果有返回值,spring初始化的時候會告訴你有個錯誤、需要設定一個proxytargetclass的某個值為true、具體就去百度google吧)

3、實現類上要有組件的注解@Component

剩下的就是corn表達式了、具體使用以及參數請百度google、

【秒】   【分】  【時】   【日】  【月】   【周】  【年】   

下面只例出幾個式子

CRON表達式    含義 
"0 0 12 * * ?"    每天中午十二點觸發 
"0 15 10 ? * *"    每天早上10:15觸發 
"0 15 10 * * ?"    每天早上10:15觸發 
"0 15 10 * * ? *"    每天早上10:15觸發 
"0 15 10 * * ? 2005"    2005年的每天早上10:15觸發 
"0 * 14 * * ?"    每天從下午2點開始到2點59分每分鍾一次觸發 
"0 0/5 14 * * ?"    每天從下午2點開始到2:55分結束每5分鍾一次觸發 
"0 0/5 14,18 * * ?"    每天的下午2點至2:55和6點至6點55分兩個時間段內每5分鍾一次觸發 
"0 0-5 14 * * ?"    每天14:00至14:05每分鍾一次觸發 
"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44觸發 
"0 15 10 ? * MON-FRI"    每個周一、周二、周三、周四、周五的10:15觸發 

Scheduled類的可配置屬性項:

String cron
A cron-like expression, extending the usual UN*X definition to include triggers on the second as well as minute, hour, day of month, month and day of week.
long fixedDelay
Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
String fixedDelayString
Execute the annotated method with a fixed period between the end of the last invocation and the start of the next.
long fixedRate
Execute the annotated method with a fixed period between invocations.
String fixedRateString
Execute the annotated method with a fixed period between invocations.
long initialDelay
Number of milliseconds to delay before the first execution of a  fixedRate() or  fixedDelay() task.
String initialDelayString
Number of milliseconds to delay before the first execution of a  fixedRate() or  fixedDelay() task.
String zone
A time zone for which the cron expression will be resolved.

以上內容基於Spring 3.0.5 版本運行,參考文檔為spring-framework-reference-3.0.5.pdf


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM