170627、springboot编程之定时任务

时间:2023-03-09 17:31:58
170627、springboot编程之定时任务

springboot定时任务,比较简单!

1、编写DemoSchedule.java类

package com.rick.common.schedule;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled; import java.text.SimpleDateFormat;
import java.util.Calendar; /**
* Desc : 定时任务
* User : RICK
* Time : 2017/8/21 21:16
*/
@Configuration
@EnableScheduling
public class DemoSchedule { //cron表达式 秒分时 日月周 年
@Scheduled(cron = "0/10 * * * * ?") //每10秒执行一次
public void demoPrint(){
System.out.println("现在时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
}
}

2、启动项目测试

170627、springboot编程之定时任务

3、项目清单

170627、springboot编程之定时任务