springboot跑定时任务

使用@Scheduled注解实现

1、在启动类上加上@EnableScheduling 开启定时任务

2、新建一个任务类,在方法上添加@Scheduled注解

@Component
public class CostStateTask {

@Scheduled(cron="0 19 9 * * ?")
public void test(){
System.out.println("我是定时任务");
}

}
解析:@Scheduled注解有多种使用方式
常用的是
  1、fixedRate(5000)-------------------5000毫秒执行一次
  2、cron="0 19 9 * * ?" -----------------每天9点19分执行一次
其中

一个cron表达式有多个

按顺序依次为

  •  秒(0~59)
  •  分钟(0~59)
  •  小时(0~23)
  •  天(月)(0~31,但是你需要考虑你月的天数)
  •  月(0~11)
  •  天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
  •  年份(1970-2099)
至于其他的时间表达式,可阅读下面的连接
参考:http://www.cnblogs.com/eoooxy/p/7099320.html
 
原文地址:https://www.cnblogs.com/memoa/p/10314025.html