SpringBoot创建定时任务

  之前总结过spring+quartz实现定时任务的整合http://www.cnblogs.com/gdpuzxs/p/6663725.html,而springboot创建定时任务则是相当简单。

  (1)在springboot主类中@EnableScheduling注解,启用定时任务的配置,如下:

  

  (2)创建定时任务实现类,如下:

package springboot.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class ScheduledTasks {
    
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
    @Scheduled(cron="0 */1 * * * ?")
    public void reportCurrentTime() {
        
        System.out.println("每一分钟执行一次:" + dateFormat.format(new Date()));
    }

}

  执行结果,如下:

  

原文地址:https://www.cnblogs.com/gdpuzxs/p/7191504.html