java使用@Scheduled注解執(zhí)行定時任務
更新時間:2021年01月13日 14:46:16 作者:程序員大本營
這篇文章主要給大家介紹了關于java使用@Scheduled注解執(zhí)行定時任務的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
前言
在寫項目的時候經常需要特定的時間做一些特定的操作,尤其是游戲服務器,維護線程之類的,這時候就需要用到定時器。
如果此時你剛好用的是spring的話,哪么@Scheduled注解是非常好用的。
使用spring @Scheduled注解執(zhí)行定時任務:
1,在spring-MVC.xml文件中進行配置

2,直接在代碼控制層使用即可
package xkhd.game.fix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 游戲數據表維護
*
* @author Administrator
*
*/
@Component
@Lazy(value = false)
public class fix_game {
@Autowired
private fix_Service fix_Service;
/**
* 每分鐘
*/
@Scheduled(cron = "0 */1 * * * ?")
public void Everyminute_control() {
System.out.println("***********每分鐘");
fix_Service.Everyminute();
}
/**
* 每小時
*/
@Scheduled(cron = "0 0 0/1 * * ?")
public void Everyhours_control() {
System.out.println("***********每小時");
fix_Service.Everyhours();
fix_Service.deleteUserlogincodeCt();
fix_Service.weixin();
}
/**
* 每天零點
*/
@Scheduled(cron = "0 0 0 * * ?")
public void Everyday_control() {
System.out.println("***********每天零點");
fix_Service.Morningeveryday();
}
}
上面是一些項目中的源碼,僅供參考。
總結
到此這篇關于java使用@Scheduled注解執(zhí)行定時任務的文章就介紹到這了,更多相關java @Scheduled注解執(zhí)行定時任務內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
mybatis-plus插入一條數據,獲取插入數據自動生成的主鍵問題
這篇文章主要介紹了mybatis-plus插入一條數據,獲取插入數據自動生成的主鍵問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
結合線程池實現apache?kafka消費者組的誤區(qū)及解決方法
這篇文章主要介紹了結合線程池實現apache?kafka消費者組的誤區(qū)及解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07

