1. 程式人生 > >@EnableScheduling和@Scheduled、@EnableAsync和@Async(1)

@EnableScheduling和@Scheduled、@EnableAsync和@Async(1)

今天在瀏覽公司專案的時候看到了兩個以前沒有接觸到的註解分別是@EnableScheduling和EnableAsync。先簡單說一下我們公司的專案框架吧,我們團隊的服務端專案框架使用的是SpringBoot框架,基本上利用了SpringBoot的大部分元件來進行開發的。今天看到的這兩個註解就是標註在SpringBoot的主程式入口類上,類似如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@EnableAsync
@SpringBootApplication
public class TestApplication {

    public static void main(String[] args) {

        SpringApplication.run(TestApplication.class, args);

    }
}

那麼這兩個註解分別表示什麼意思呢?在經過一番查閱之後,總結如下:

1、@EnableScheduling和@Scheduled

@EnableScheduling:該註解標註在配置類上,用於開啟對定時任務的支援。

其中Scheduled註解中有以下幾個引數:

cron:是設定定時執行的表示式,如 0/5 * * * ?每隔五分鐘執行一次;裡面的引數(秒 分 小時 日期 月份 星期 年 )

zone:表示執行時間的時區;

fixedDelay和fixedDelayString:表示一個固定延遲時間執行,上個任務完成後,延遲多長時間執行;

fixedRate和fixedRateString:表示一個固定頻率執行,上個任務開始後,多長時間後開始執行;

initialDelay和initialDelayString:表示一個初始延遲時間,第一次被呼叫前延遲的時間。

@Scheduled:該註解標註在具體的方法上,用於宣告具體要執行的定時任務。

配置類:

package com.schedule.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * 配置類
 */
@Configuration
@Component
@EnableScheduling
public class ScheduledConfig {

}

具體業務類:

package com.schedule.service;

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

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduledService {

    private static final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");

    //每5秒一次執行一次該方法
    @Scheduled(cron = "0/5 * * * * ?")
    public void cronTest(){
        System.out.println("cron,當前時間:"+ format.format(new Date()));
    }

    //初始延遲1秒鐘,每隔2秒鐘執行一次該方法
    @Scheduled(fixedRateString = "2000", initialDelay = 1000)
    public void fixedRateTest(){
        System.out.println("fixedRateString,當前時間:" +format.format(new Date()));
    }

    //每次執行完該方法便延遲兩秒鐘
    @Scheduled(fixedDelayString = "2000")
    public void fixedDelayTest(){
        System.out.println("fixedDelayString,當前時間:" +format.format(new Date()));
    }
}

測試:

package com.schedule;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ScheduledApplication {
    public static void main(String[] args) {

        SpringApplication.run(ScheduledApplication.class, args);

    }
}

結果:

fixedRateString,當前時間:23:51:22
fixedDelayString,當前時間:23:51:23
fixedRateString,當前時間:23:51:24
cron,當前時間:23:51:25
fixedDelayString,當前時間:23:51:25
fixedRateString,當前時間:23:51:26
fixedDelayString,當前時間:23:51:27
fixedRateString,當前時間:23:51:28
fixedDelayString,當前時間:23:51:29
cron,當前時間:23:51:30
fixedRateString,當前時間:23:51:30
。。。。

本文暫且說到這裡,下一節,我將討論第二組註解@EnableAsync和@Async。