java使用@Scheduled注解执行定时任务

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java使用@Scheduled注解执行定时任务

程序员大本营   2021-01-13 我要评论
这篇文章主要给大家介绍了关于java使用@Scheduled注解执行定时任务的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

在写项目的时候经常需要特定的时间做一些特定的操作,尤其是游戏服务器,维护线程之类的,这时候就需要用到定时器。

如果此时你刚好用的是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();
 }

}

上面是一些项目中的源码,仅供参考。

总结

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们