@Component public class FlightTrainTask { @Scheduled(cron = "0/5 * * * * ? ") // 间隔5秒执行 public void taskCycle() { System.out.println("使用SpringMVC框架配置定时任务"); } }
https://blog.csdn.net/qidasheng2012/article/details/84386662
cron一共有7位,但是最后一位是年(1970-2099),可以留空,所以我们可以写6位,按顺序依次为
秒
(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
星期(1~7 1=SUN,MON,TUE,WED,THU,...
“0 15 10 * * ?” 每天上午10:15触发
“0 15 10 * * ? *” 每天上午10:15触发
“0 15 10 * * ? 2005” 2005年的每天上午10:15触发
“0 * 14 * * ?”
之前转载过一篇使用Timer定时器完成
定时任务
,这次我们使用@Scheduled注解来实现定时更新数据库数据
在
Spring
Boot项目中,由于使用注解替代了大量的配置文件,所以直接使用该注解即可。
在
MVC
框架下,则需要配置一些数据。
在
spring
的配置xml文件中添加以下内容
xmlns中加入
xmlns:task="http://www.
spring
framework.org/schema/task"
xsi:schemaLocation中加入
http://www.spri..
import org.
spring
framework.scheduling.annotation.Scheduled;
import org.
spring
framework.stereotype.Component;
@Component
public class MyTask {
@Scheduled(fixedRate = 30000)
public void executeTask() {
//
执行
任务的代码
在上面的代码中,@Scheduled注解指定了任务的
执行
频率为每30
秒
一次
,executeTask()方法中编写了具体的任务代码。