av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot中使用@Scheduled注解創(chuàng)建定時(shí)任務(wù)的實(shí)現(xiàn)

瀏覽:2日期:2023-05-10 08:40:30

在項(xiàng)目日常開(kāi)發(fā)過(guò)程中,經(jīng)常需要定時(shí)任務(wù)來(lái)幫我們做一些工作,如清理日志。定時(shí)任務(wù)的實(shí)現(xiàn)方法主要有 Timer、Quartz 以及 elastic-job

Timer 實(shí)現(xiàn)定時(shí)任務(wù)

只執(zhí)行一次的定時(shí)任務(wù)

Timer timer = new Timer();timer.schedule(new TimerTask() { @Override public void run() { System.out.println('2000毫米后執(zhí)行一次。'); }}, 2000);timer.schedule(new TimerTask() { @Override public void run() { System.out.println('5000毫米后執(zhí)行一次。'); }}, new Date(System.currentTimeMillis() + 5000));

循環(huán)執(zhí)行任務(wù)

Timer timer = new Timer();timer.schedule(new TimerTask() { @Override public void run() { System.out.println(111); }}, 1000, 2000); // 1000毫米后執(zhí)行第一次,之后每2000毫米執(zhí)行一次

終止任務(wù)

timer.concel();

Timer 是 JDK 實(shí)現(xiàn)的定時(shí)任務(wù),用起來(lái)簡(jiǎn)單、方便,對(duì)一些簡(jiǎn)單的定時(shí)任務(wù)可以使用它。由于它不支持 cron 表達(dá)式,現(xiàn)在已經(jīng)很少用了。

Quartz 實(shí)現(xiàn)定時(shí)任務(wù)

Quartz 是一個(gè)完全由 Java 編寫(xiě)的開(kāi)源作業(yè)調(diào)度框架,可以用它來(lái)實(shí)現(xiàn)定時(shí)任務(wù)。

在 pom.xml 文件添加 Quartz 依賴(lài)

<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version></dependency><dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version></dependency><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.25</version></dependency><dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.6</version></dependency>

編寫(xiě) Job

定時(shí)執(zhí)行的任務(wù)

public class QuartzJob implements Job{ public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); String hello = (String) jobDataMap.get('hello'); System.err.println(hello); } }

編寫(xiě) Task

public void task() { // 該 map 可在 job 中獲取 JobDataMap map = new JobDataMap(); map.put('hello', 'world'); JobDetail jobDetail = newJob(QuartzJob.class). withIdentity('myJob', 'myGroup'). setJobData(map).build(); /* * 簡(jiǎn)單定時(shí)器 * * 執(zhí)行時(shí)間間隔 * withIntervalInMilliSeconds 毫秒 * withIntervalInSeconds 秒 * withIntervalInMinutes 分鐘 * withIntervalInHours 小時(shí) * * 執(zhí)行次數(shù) * repeatForever 重復(fù)執(zhí)行 * withRepeatCount 次數(shù) */ SimpleScheduleBuilder scheduleBuilder = simpleSchedule().withIntervalInSeconds(3).withRepeatCount(10); /* * corn定時(shí)器 * * corn表達(dá)式,使用更靈活 * corn表達(dá)式在線(xiàn)生成 http://cron.qqe2.com/ */ CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule('0 0 0 1 * ?'); Trigger trigger = newTrigger().startAt(new Date()).//startNow() 默認(rèn)現(xiàn)在開(kāi)始 withIdentity('myTrigger', 'myGroup'). //withSchedule(scheduleBuilder).build(); withSchedule(cronScheduleBuilder).build(); try { //1.創(chuàng)建Scheduler工廠(chǎng) SchedulerFactory schedulerFactory = new StdSchedulerFactory(); //2.獲取實(shí)例 Scheduler scheduler = schedulerFactory.getScheduler(); //3.設(shè)置jobDetail詳情和trigger觸發(fā)器 scheduler.scheduleJob(jobDetail, trigger); //4.定時(shí)任務(wù)開(kāi)始 scheduler.start(); } catch (SchedulerException e) { e.printStackTrace(); }}

在項(xiàng)目啟動(dòng)的時(shí)候調(diào)用 task 方法即可啟動(dòng)定時(shí)任務(wù)。

Spring Boot 創(chuàng)建定時(shí)任務(wù)

Spring Boot 默認(rèn)已經(jīng)實(shí)現(xiàn)了定時(shí)任務(wù),只需要添加相應(yīng)的注解即可完成

pom.xml 文件配置

pom.xml 不需要添加其他依賴(lài),只需要加入 Spring Boot 依賴(lài)即可,這里我們添加一個(gè) web 和 test 的依賴(lài)

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency></dependencies>

在啟動(dòng)類(lèi)上面加上 @EnableScheduling 注解

在啟動(dòng)類(lèi)上面加上 @EnableScheduling 注解即可開(kāi)啟定時(shí)任務(wù)

@EnableScheduling@SpringBootApplicationpublic class SchedulingApplication { public static void main(String[] args) { SpringApplication.run(SchedulingApplication.class, args); }}

編寫(xiě)定時(shí)任務(wù)

@Componentpublic class ScheduledTask { @Scheduled(initialDelay=1000, fixedDelay = 1000) public void task1() { System.out.println('延遲1000毫秒后執(zhí)行,任務(wù)執(zhí)行完1000毫秒之后執(zhí)行!'); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } @Scheduled(fixedRate = 2000) public void task2() { System.out.println('延遲1000毫秒后執(zhí)行,之后每2000毫秒執(zhí)行一次!'); }}

除了這些還支持 cron 表達(dá)式

@Scheduled(cron = '*/2 * * * * ?')public void task3() { System.out.println('每2秒執(zhí)行一次!');}

啟動(dòng) Spring Boot 項(xiàng)目在控制臺(tái)就會(huì)看到任務(wù)定時(shí)執(zhí)行

cron 表達(dá)式

以下是 cron 表達(dá)式的的兩種語(yǔ)法

Seconds Minutes Hours DayofMonth Month DayofWeek YearSeconds Minutes Hours DayofMonth Month DayofWeek

每一個(gè)域可出現(xiàn)的字符如下:

Seconds:可出現(xiàn)', - * /'四個(gè)字符,有效范圍為0-59的整數(shù) Minutes:可出現(xiàn)', - * /'四個(gè)字符,有效范圍為0-59的整數(shù) Hours:可出現(xiàn)', - * /'四個(gè)字符,有效范圍為0-23的整數(shù) DayofMonth:可出現(xiàn)', - * / ? L W C'八個(gè)字符,有效范圍為0-31的整數(shù) Month:可出現(xiàn)', - * /'四個(gè)字符,有效范圍為1-12的整數(shù)或JAN-DEc DayofWeek:可出現(xiàn)', - * / ? L C #'四個(gè)字符,有效范圍為1-7的整數(shù)或SUN-SAT兩個(gè)范圍。1表示星期天,2表示星期一, 依次類(lèi)推 Year:可出現(xiàn)', - * /'四個(gè)字符,有效范圍為1970-2099年

舉幾個(gè)例子

*/2 * * * * ? 表示每2秒執(zhí)行一次!0 0 2 1 * ? * 表示在每月的1日的凌晨2點(diǎn)調(diào)度任務(wù) 0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15執(zhí)行作業(yè) 0 15 10 ? 6L 2002-2006 表示2002-2006年的每個(gè)月的最后一個(gè)星期五上午10:15執(zhí)行作

推薦一個(gè) cron 表達(dá)式在線(xiàn)生成工具

http://tools.jb51.net/code/Quartz_Cron_create

參考資料

本文所有代碼放在 Github 上

到此這篇關(guān)于SpringBoot中使用@Scheduled注解創(chuàng)建定時(shí)任務(wù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot創(chuàng)建定時(shí)任務(wù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 精品日韩 | 日本成人福利 | 亚洲国产中文在线 | 99re视频在线观看 | 91精品国产自产在线老师啪 | 亚洲精品一区二区久 | 日韩 欧美 综合 | 亚洲国产高清高潮精品美女 | 亚洲在线一区二区 | v亚洲 | 欧美成人免费在线视频 | 欧美日本一区二区 | 一区二区福利视频 | jizz在线看片 | 一区二区三区欧美 | 日本亚洲欧美 | 亚洲一区在线观看视频 | 日韩成人在线视频 | 欧产日产国产精品99 | 久久久久久国产 | 日本一区高清 | 亚洲一区中文 | 日韩在线精品视频 | 亚洲一区二区视频 | 国产成人久久 | 精精国产xxxx视频在线 | 久久高清 | 精品亚洲一区二区 | 国产日韩一区 | 天天综合网永久 | 欧美网址在线观看 | 日日操网站 | 亚洲狠狠 | 91在线电影 | 欧美精品在线免费观看 | 国产精品亚洲精品久久 | 亚洲成人免费 | 中文字幕第100页 | 美国av毛片| 久草视频在线播放 | 久久精品屋 |