Spring TaskScheduler使用實例解析
更新時間:2019年11月29日 10:51:00 作者:吉利DarJeely
這篇文章主要介紹了Spring TaskScheduler使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這篇文章主要介紹了Spring TaskScheduler使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
TaskScheduler
- 提供對計劃任務提供支持;
- 使用@EnableScheduling開啟計劃任務支持
- 使用@Scheduled來注解計劃任務的方法;
示例
演示后臺間斷執(zhí)行任務和定時計劃任務
計劃任務的配置
@Configuration
@EnableScheduling
public class DemoConfig {
}
計劃配置任務類
package com.wisely.task.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class DemoScheduledTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000) //每五秒執(zhí)行一次
public void reportCurrentTime() {
System.out.println("每隔五秒執(zhí)行一次 " + dateFormat.format(new Date()));
}
@Scheduled(cron = "0 22 11 ? * *" ) //每天上午11點22執(zhí)行
public void fixTimeExecution(){
System.out.println("在指定時間 " + dateFormat.format(new Date())+"執(zhí)行");
}
}
測試
package com.wisely.task.scheduler;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
@SuppressWarnings({ "unused", "resource" })
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.task.scheduler");
}
}
輸出結果
每隔五秒執(zhí)行一次 11:21:42 每隔五秒執(zhí)行一次 11:21:47 每隔五秒執(zhí)行一次 11:21:52 每隔五秒執(zhí)行一次 11:21:57 在指定時間 11:22:00執(zhí)行 每隔五秒執(zhí)行一次 11:22:02
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- SpringBoot2 task scheduler 定時任務調度器四種方式
- Spring Boot利用@Async異步調用:ThreadPoolTaskScheduler線程池的優(yōu)雅關閉詳解
- spring中定時任務taskScheduler的詳細介紹
- spring定時任務(scheduler)的串行、并行執(zhí)行實現(xiàn)解析
- Spring @Scheduler使用cron表達式時的執(zhí)行問題詳解
- mall整合SpringTask實現(xiàn)定時任務的方法示例
- Spring線程池ThreadPoolTaskExecutor配置詳情
- Spring Task定時任務的配置和使用詳解
相關文章
Java動態(tài)代理機制詳解_動力節(jié)點Java學院整理
這篇文章主要為大家詳細介紹了Java動態(tài)代理機制,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
SpringCloud+MyBatis分頁處理(前后端分離)
這篇文章主要為大家詳細介紹了SpringCloud+MyBatis分頁處理,前后端分離,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10
springboot如何使用redis的incr創(chuàng)建分布式自增id
這篇文章主要介紹了springboot如何使用redis的incr創(chuàng)建分布式自增id,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

