當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
@scheduled只执行一次_SpringBoot第四篇:定时任务@Scheduled
生活随笔
收集整理的這篇文章主要介紹了
@scheduled只执行一次_SpringBoot第四篇:定时任务@Scheduled
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用SpringBoot創建定時任務主要有三種創建方式:
1.基于注解(@Scheduled)
2.基于接口(SchedulingConfigurer)實際使用中我們往往想從數據庫中讀取指定時間來動態執行定時任務,這時候基于接口的定時任務就派上用場了。
3.基于注解設定多線程定時任務
本篇只介紹基于注解開啟定時任務
4.1新建定時任務
在SpringBoot第三篇:整合MyBatis-Plus基礎上新建一個Task包,在Task新建SaticScheduleTask
package com.firstwave.sfspringboot.Task;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import java.time.LocalDateTime;/** * @Author: shafei * @Date: 2020-05-21 10:24 * @Version: V1.0 * @Description: */@Configuration //標記配置類,兼備Component的效果。@EnableScheduling //開啟定時任務public class SaticScheduleTask { //添加定時任務 @Scheduled(cron = "0/5 * * * * ?") //或直接指定時間間隔,例如:5秒 //@Scheduled(fixedRate=5000) private void configureTasks() { System.out.println("執行靜態定時任務時間: " + LocalDateTime.now()); }}@EnableScheduling 注解放在啟動類也是可以的
4.2項目結構截圖:
@EnableScheduling 注解放在啟動類也是可以的
4.2項目結構截圖:
4.3啟動項目查看 控制臺:
4.4源碼:
https://gitee.com/shafeipersional/sf-springboot/tree/master/springboot3/sf-springboot
總結
以上是生活随笔為你收集整理的@scheduled只执行一次_SpringBoot第四篇:定时任务@Scheduled的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 读取无线手柄数据_《宇宙机器人》×PS5
- 下一篇: redis 连接池_SpringBoot