spring boot: 计划任务@ EnableScheduling和@Scheduled

spring boot: 计划任务@ EnableScheduling和@Scheduled

@Scheduled中的参数说明

@Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;

@Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;

@Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;

@Scheduled(cron="* * * * * ?"):按cron规则执行。

  

常用Cron表达式(  秒/分/时/日/月/周/年  )

0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 
0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时 
0 0 12 ? * WED 表示每个星期三中午12点 
"0 0 12 * * ?" 每天中午12点触发 
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
"0 15 10 15 * ?" 每月15日上午10:15触发 
"0 15 10 L * ?" 每月最后一日的上午10:15触发 
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发 
"0 15 10 ? * *" 每天上午10:15触发 
"0 15 10 * * ?" 每天上午10:15触发 
"0 15 10 * * ? *" 每天上午10:15触发 
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 

  

实例:

service

 1 package ch2.scheduler;
 2 
 3 //时间处理,时间格式化
 4 import java.util.Date;
 5 import java.text.SimpleDateFormat;
 6 
 7 
 8 //spring计划任务声明(针对方法声明)
 9 import org.springframework.scheduling.annotation.Scheduled;
10 //spring组件声明
11 import org.springframework.stereotype.Service;
12 
13 
14 //组件什么
15 @Service
16 public class SchedulerService {
17 
18     //创建日期模板
19     private static final SimpleDateFormat  dateFormat = new SimpleDateFormat("HH::MM::ss");
20     
21     
22     //每5秒钟执行一次
23     @Scheduled(fixedRate = 5000)
24     public void reportCurrentTime()
25     {
26         System.out.println("每五秒执行一次: " + dateFormat.format( new Date() ));
27     }
28     
29     //按照cron属性指定执行时间:秒分时
30     @Scheduled(cron = "0 34 18 ? * *")
31     public void fixTimeExecution()
32     {
33         System.out.println("在指定的时间内执行: " + dateFormat.format( new Date()) );
34     }
35     
36     
37 }

 

config

 1 package ch2.scheduler;
 2 
 3 //自动引入包
 4 import org.springframework.context.annotation.ComponentScan;
 5 //配置类声明
 6 import org.springframework.context.annotation.Configuration;
 7 
 8 //计划任务类声明
 9 import org.springframework.scheduling.annotation.EnableScheduling;
10 
11 
12 //配置类声明
13 @Configuration
14 //自动引入包
15 @ComponentScan("ch2.scheduler")
16 //通过@EnableScheduling开启对计划任务的支持
17 @EnableScheduling
18 public class TaskSchedulerConfig {
19 
20     
21     
22 }

main

 1 package ch2.scheduler;
 2 //引入容器
 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 4 
 5 public class Main {
 6 
 7     
 8     public static void main(String[] args)
 9     {
10         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
11         //SchedulerService schedulerService  = context.getBean(SchedulerService.class);
12         
13         
14     }
15 }

 

posted @ 2018-01-02 18:46  穆晟铭  阅读(1654)  评论(0编辑  收藏  举报