Spring計(jì)劃任務(wù)用法實(shí)例詳解
本文實(shí)例講述了Spring計(jì)劃任務(wù)用法。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
從Spring3.1開始,計(jì)劃任務(wù)在Spring中的實(shí)現(xiàn)變得異常的簡(jiǎn)單。只需要下面兩步。
1 通過在配置類上注解@EnableScheduling來(lái)開啟對(duì)計(jì)劃任務(wù)的支持。
2 在要執(zhí)行計(jì)劃任務(wù)的方法上注解@Scheduled,聲明這是一個(gè)計(jì)劃任務(wù)。
Spring通過@Scheduled支持多種類型的計(jì)劃任務(wù),包含cron、fixDelay、fixRate等。
二 實(shí)戰(zhàn)
1 配置類
package com.wisely.highlight_spring4.ch3.taskscheduler; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @Configuration @ComponentScan("com.wisely.highlight_spring4.ch3.taskscheduler") @EnableScheduling //1 public class TaskSchedulerConfig { }
2 計(jì)劃任務(wù)執(zhí)行類
package com.wisely.highlight_spring4.ch3.taskscheduler; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class ScheduledTaskService { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) //該方法是計(jì)劃任務(wù),使用fixedRate屬性每隔固定時(shí)間執(zhí)行。 public void reportCurrentTime() { System.out.println("每隔五秒執(zhí)行一次 " + dateFormat.format(new Date())); } @Scheduled(cron = "0 28 11 ? * *" ) //每天11點(diǎn)28分執(zhí)行 public void fixTimeExecution(){ System.out.println("在指定時(shí)間 " + dateFormat.format(new Date())+"執(zhí)行"); } }
3 主類
package com.wisely.highlight_spring4.ch3.taskscheduler; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class); } }
三 運(yùn)行結(jié)果
每隔五秒執(zhí)行一次 19:58:50
每隔五秒執(zhí)行一次 19:58:55
每隔五秒執(zhí)行一次 19:59:00
每隔五秒執(zhí)行一次 19:59:05
每隔五秒執(zhí)行一次 19:59:10
每隔五秒執(zhí)行一次 19:59:15
每隔五秒執(zhí)行一次 19:59:20
每隔五秒執(zhí)行一次 19:59:25
每隔五秒執(zhí)行一次 19:59:30
每隔五秒執(zhí)行一次 19:59:35
每隔五秒執(zhí)行一次 19:59:40
每隔五秒執(zhí)行一次 19:59:45
每隔五秒執(zhí)行一次 19:59:50
每隔五秒執(zhí)行一次 19:59:55
每隔五秒執(zhí)行一次 20:00:00
每隔五秒執(zhí)行一次 20:00:05
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring5源碼解析之Spring中的異步和計(jì)劃任務(wù)
- 詳解在Spring3中使用注解(@Scheduled)創(chuàng)建計(jì)劃任務(wù)
- springboot與mybatis整合實(shí)例詳解(完美融合)
- Spring MVC 框架搭建配置方法及詳解
- Spring中的事務(wù)管理實(shí)例詳解
- 詳解eclipse下創(chuàng)建第一個(gè)spring boot項(xiàng)目
- 詳解Java的Spring框架中的注解的用法
- SpringMVC文件上傳 多文件上傳實(shí)例
- springboot如何讀取配置文件(application.yml)中的屬性值
相關(guān)文章
初識(shí)sa-token及登錄授權(quán)簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家介紹了sa-token及登錄授權(quán)簡(jiǎn)單實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07springboot v2.0.3版本多數(shù)據(jù)源配置方法
這篇文章主要介紹了springboot v2.0.3版本多數(shù)據(jù)源配置方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-11-11Java實(shí)現(xiàn)鏈表數(shù)據(jù)結(jié)構(gòu)的方法
這篇文章主要介紹了Java實(shí)現(xiàn)鏈表數(shù)據(jù)結(jié)構(gòu)的相關(guān)資料,每一個(gè)鏈表都包含多個(gè)節(jié)點(diǎn),節(jié)點(diǎn)又包含兩個(gè)部分,一個(gè)是數(shù)據(jù)域(儲(chǔ)存節(jié)點(diǎn)含有的信息),一個(gè)是引用域(儲(chǔ)存下一個(gè)節(jié)點(diǎn)或者上一個(gè)節(jié)點(diǎn)的地址),需要的朋友可以參考下2022-01-01MyBatis如何通過xml方式實(shí)現(xiàn)SaveOrUpdate
這篇文章主要講如何通過xml方式實(shí)現(xiàn)SaveOrUpdate,但是仍然建議在Service中實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-06-06