網站首頁 編程語言 正文
1.開啟定時任務注解
在類上添加注解:@EnableScheduling
2.cron表達式
@Scheduled(cron = "0 0 10 * * ? ")
實例:
package com.ynkbny.enterprise.scheduled; import com.ynkbny.enterprise.service.CarbonEmissionsService; import com.ynkbny.enterprise.service.LaoGouService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.time.LocalDate; /** * @title 自動任務 * @description 自動任務 * @version 1.0.0 * @create 2023/12/4 **/ @Component @EnableScheduling public class ScheduledTask { private final Logger logger = LoggerFactory.getLogger(ScheduledTask.class); @Resource private LaoGouService laoGouService; @Resource private CarbonEmissionsService carbonEmissionsService; @Bean public TaskScheduler taskScheduler() { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); //我這里設置的線程數是18,可以根據需求調整 taskScheduler.setPoolSize(18); return taskScheduler; } /** * 存儲 查詢項目列表 * * 每天1點執行 * */ @Scheduled(cron = "0 0 1 * * ? ") public void queryProjectList() { //測試 (cron="*/10 * * * * ?") 代表10s執行一次 try { logger.info("====存儲 查詢項目列表 開始===="); laoGouService.queryProjectList(); }catch (Exception e) { logger.error("====存儲 查詢項目列表{}====", e.getMessage()); } logger.info("====存儲 查詢項目列表 結束===="); } /** * 存儲 查詢“項目表”項目的設備列表 * * 每天3點執行 */ @Scheduled(cron = "0 0 3 * * ? ") public void queryDevice() { //測試 (cron="*/10 * * * * ?") 代表10s執行一次 try { logger.info("====存儲 查詢“項目表”項目的設備列表 開始===="); laoGouService.queryDevice(); }catch (Exception e) { logger.error("====存儲 查詢“項目表”項目的設備列表{}====", e.getMessage()); } logger.info("====存儲 查詢“項目表”項目的設備列表 結束===="); } }
原文鏈接:https://blog.csdn.net/m0_58660225/article/details/134786888
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-12-05 一文深入了解Python中的繼承知識點_python
- 2022-10-27 C#利用Spire.Pdf包實現為PDF添加數字簽名_C#教程
- 2023-03-17 Pandas檢查dataFrame中的NaN實現_python
- 2022-10-23 Android用于加載xml的LayoutInflater源碼超詳細分析_Android
- 2022-07-21 windows與Linux查看端口占用并終止端口占用
- 2021-12-18 死鎖的處理基本策略和常用方法
- 2023-07-08 git代碼回滾到某個tag
- 2022-10-29 在Centos8中安裝配置Redis,實現遠程訪問
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支