您的位置:首页 > 编程语言 > Java开发

SpringBoot创建定时任务

2017-07-16 19:37 351 查看
  之前总结过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()));
}

}


  执行结果,如下:

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: