springboot中如何使用@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)-創(chuàng)新互聯(lián)

springboot中如何使用@Scheduled實(shí)現(xiàn)定時(shí)任務(wù),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的羅定網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

1、pom.xml中導(dǎo)入必要的依賴:

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>2.0.1.RELEASE</version>  </parent>  <dependencies>    <!-- SpringBoot 核心組件 -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-tomcat</artifactId>    </dependency>  </dependencies>

2、寫(xiě)一個(gè)springboot的啟動(dòng)類:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan;import org.springframework.scheduling.annotation.EnableScheduling;@ComponentScan(basePackages = { "com.xwj.tasks" })@EnableScheduling // 開(kāi)啟定時(shí)任務(wù)@EnableAutoConfigurationpublic class App {  public static void main(String[] args) {    SpringApplication.run(App.class, args);  }}

注意這里一定要加上@EnableScheduling注解,用于開(kāi)啟定時(shí)任務(wù)

3、開(kāi)始寫(xiě)定時(shí)任務(wù):

import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class ScheduleTask {  @Scheduled(fixedRate = 1000)  // @Scheduled(cron = "0 23-25 18 * * ?")  public void testSchedule() {    System.out.println("定時(shí)任務(wù):" + System.currentTimeMillis());  }}

解釋:

@Scheduled注解:

1、fixedRate 以固定速率執(zhí)行。以上表示每隔1秒執(zhí)行一次

2、fixedDelay 以上一個(gè)任務(wù)開(kāi)始時(shí)間為基準(zhǔn),從上一任務(wù)開(kāi)始執(zhí)行后再次調(diào)用

3、cron表達(dá)式??梢詫?shí)現(xiàn)定時(shí)調(diào)用。

在使用的過(guò)程中,樓主覺(jué)得,如果只有一個(gè)定時(shí)任務(wù),fixedRate與fixedDelay的效果是一樣一樣的

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

網(wǎng)站題目:springboot中如何使用@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://bm7419.com/article14/ddjgge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、手機(jī)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、域名注冊(cè)云服務(wù)器、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司