前言
在写项目的时候经常需要特定的时间做一些特定的操作,尤其是游戏服务器,维护线程之类的,这时候就需要用到定时器。
如果此时你刚好用的是spring的话,哪么@scheduled注解是非常好用的。
使用spring @scheduled注解执行定时任务:
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注解执行定时任务的文章就介绍到这了,更多相关java @scheduled注解执行定时任务内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!
希望与广大网友互动??
点此进行留言吧!