本文共 1170 字,大约阅读时间需要 3 分钟。
工程中经常会用到计划任务,Spring Boot 中启用计划任务通过@EnableScheduling
、@Scheduled
两个注解就可以搞定。
在Spring Boot启动类加上注解@EnableScheduling
,表示该工程启用计划任务
@EnableScheduling@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); }}
在计划任务方法上加上@Scheduled
注解,表示该方法是一个计划任务,工程启动后会去在找加上该注解的方法加入计划任务列表。
/** * 计划任务类 * Created by lujun.chen on 2017/2/25. */@Componentpublic class CljSchedule { /** * 每五分钟执行一次 */ @Scheduled(cron = "0/5 * * * * ?") public void timer1(){ System.out.println(DateFormatUtils.ISO_8601_EXTENDED_DATETIME_FORMAT.format(new Date())); }}
@Scheduled
注解有一些参数,用于配置计划任务执行频率,执行时段等。
e.g. {@code "0 * * * * ?"}
从前到后依次表示秒 分 时 日 月 年转载地址:http://cuvmb.baihongyu.com/