1. 程式人生 > >SpringBoot創建定時任務

SpringBoot創建定時任務

epo .html tex sys private tasks 簡單 http final

  之前總結過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())); } }

  執行結果,如下:

  技術分享

SpringBoot創建定時任務