很多時候,我們有這么一個需求,需要在每天的某個固定時間或者每隔一段時間讓應(yīng)用去執(zhí)行某一個任務(wù)。為了實(shí)現(xiàn)這個需求,通常我們會通過多線程來實(shí)現(xiàn)這個功能,但是這樣我們需要自己做一些比較麻煩的工作。接下來,讓我們看看如何使用Spring scheduling task簡化定時任務(wù)功能的實(shí)現(xiàn)。
添加maven依賴
為了方便展示,我們使用Spring Boot來簡化我們的Spring配置。因?yàn)槲覀兪褂玫氖荢pring自帶的Scheduling,因此我們只需要引入最進(jìn)本的spring-boot-starter即可。
<parent>
<groupId>org.springframework.boot<span class="hljs-name"groupId>
<artifactId>spring-boot-starter-parent<span class="hljs-name"artifactId>
<version>1.2.5.RELEASE<span class="hljs-name"version>
<span class="hljs-name"parent>
<properties>
<java.version>1.8<span class="hljs-name"java.version>
<span class="hljs-name"properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot<span class="hljs-name"groupId>
<artifactId>spring-boot-starter<span class="hljs-name"artifactId>
<span class="hljs-name"dependency>
<span class="hljs-name"dependencies>
注意,Spring boot需要JDK8的編譯環(huán)境。
創(chuàng)建Scheduled Task
讓我們創(chuàng)建一個ScheduleTask來實(shí)現(xiàn)我們的需求:
@Component
public class ScheduledTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Integer count0 = 1;
private Integer count1 = 1;
private Integer count2 = 1;
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() throws InterruptedException {
System.out.println(String.format("---第%s次執(zhí)行,當(dāng)前時間為:%s", count0++, dateFormat.format(new Date())));
}
@Scheduled(fixedDelay = 5000)
public void reportCurrentTimeAfterSleep() throws InterruptedException {
System.out.println(String.format("===第%s次執(zhí)行,當(dāng)前時間為:%s", count1++, dateFormat.format(new Date())));
}
@Scheduled(cron = "*/5 * * * * *")
public void reportCurrentTimeCron() throws InterruptedException {
System.out.println(String.format("+++第%s次執(zhí)行,當(dāng)前時間為:%s", count2++, dateFormat.format(new Date())));
}
}
可以看到,我們在我們真正需要執(zhí)行的方法上添加了@Scheduled
標(biāo)注,表示這個方法是需要定時執(zhí)行的。
在@Scheduled
標(biāo)注中,我們使用了三種方式來實(shí)現(xiàn)了同一個功能:每隔5秒鐘記錄一次當(dāng)前的時間:
fixedRate = 5000
表示每隔5000ms,Spring scheduling會調(diào)用一次該方法,不論該方法的執(zhí)行時間是多少
fixedDelay = 5000
表示當(dāng)方法執(zhí)行完畢5000ms后,Spring scheduling會再次調(diào)用該方法
cron = "*/5 * * * * * *"
提供了一種通用的定時任務(wù)表達(dá)式,這里表示每隔5秒執(zhí)行一次。
配置Scheduling
接下來我們通過Spring boot來配置一個最簡單的Spring web應(yīng)用,我們只需要一個帶有main方法
的類即可:
[]()
@SpringBootApplication
@EnableScheduling
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
我們先來看看class上的標(biāo)注:
@SpringBootApplication
實(shí)際上是了以下三個標(biāo)注的集合:
@Configuration
告訴Spring這是一個配置類,里面的所有標(biāo)注了@Bean
的方法的返回值將被注冊為一個Bean
@EnableAutoConfiguration
告訴Spring基于class path的設(shè)置、其他bean以及其他設(shè)置來為應(yīng)用添加各種Bean
@ComponentScan
告訴Spring掃描Class path下所有類來生成相應(yīng)的Bean
@EnableScheduling
告訴Spring創(chuàng)建一個task executor
,如果我們沒有這個標(biāo)注,所有@Scheduled
標(biāo)注都不會執(zhí)行
通過以上標(biāo)注,我們完成了schedule的基本配置。最后,我們添加main
方法來啟動一個Spring boot應(yīng)用即可。
測試
在根目錄下執(zhí)行命令:mvn spring-boot:run
,我們可以看到:
-
Web服務(wù)器
+關(guān)注
關(guān)注
0文章
138瀏覽量
24767 -
JDK
+關(guān)注
關(guān)注
0文章
83瀏覽量
16832
發(fā)布評論請先 登錄
關(guān)于stm32系統(tǒng)定時任務(wù)的問題
Spring Boot定時任務(wù)的重寫方法
SpringBoot如何實(shí)現(xiàn)動態(tài)增刪啟停定時任務(wù)

Python定時任務(wù)的實(shí)現(xiàn)方式
說說Spring定時任務(wù)如何大規(guī)模企業(yè)級運(yùn)用
解析Golang定時任務(wù)庫gron設(shè)計(jì)和原理
求一種SpringBoot定時任務(wù)動態(tài)管理通用解決方案
SpringBoot如何實(shí)現(xiàn)定時任務(wù)(上)

Spring Boot中整合兩種定時任務(wù)的方法

在Spring Boot中如何使用定時任務(wù)
如何動態(tài)添加修改刪除定時任務(wù)?
Linux如何使用cron進(jìn)行定時任務(wù)的操作
python定時任務(wù)實(shí)踐

評論