在看Springboot常用的注解的时候,发现Springboot竟然有一个支持定时任务的注解@Scheduled
什么玩意?springboot支持定时任务?哎呦,这个高级啊。
这样以后有些定时任务就不需要配置到服务器的cron定时任务中了。
一:开启定时任务
需要在入口文件中加入注解:@EnableScheduling
// 开启定时任务
@EnableScheduling
public class EntryApplication
{
public static void main(String[] args)
{
SpringApplication.run(EntryApplication.class, args);
}
}
二:创建定时任务方法
@Scheduled 注解支持多种形式的时间配置:
- fixedRate:按照固定的时间间隔(以毫秒为单位)执行,任务开始时计算间隔。
- fixedDelay:按照固定的延迟(以毫秒为单位)执行,任务完成后计算间隔。
- cron:使用 Cron 表达式指定执行时间。
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyScheduledTasks
{
/**
* 每隔5秒执行一次
*/
@Scheduled(fixedRate = 5000)
public void executeTaskWithFixedRate()
{
System.out.println("定时任务执行时间: " + System.currentTimeMillis() / 1000);
}
/**
* 上一个定时任务完成后10秒执行
*/
@Scheduled(fixedDelay = 10000)
public void executeTaskWithFixedDelay()
{
System.out.println("上一个定时任务结束之后延迟10秒执行: " + System.currentTimeMillis() / 1000);
}
/**
* 每天15点13分执行(就是使用cron定时任务的语法)
*/
@Scheduled(cron = "0 29 15 * * ?")
public void executeTaskWithCronExpression() {
System.out.println("使用cron表达式执行的任务: " + System.currentTimeMillis() / 1000 );
}
}
运行项目,定时任务自动执行,控制台输出:
定时任务执行时间: 1725348539
使用cron表达式执行的任务: 1725348540
定时任务执行时间: 1725348544
上一个定时任务结束之后延迟10秒执行: 1725348544
定时任务执行时间: 1725348549
定时任务执行时间: 1725348554
上一个定时任务结束之后延迟10秒执行: 1725348554
定时任务执行时间: 1725348559
定时任务执行时间: 1725348564
上一个定时任务结束之后延迟10秒执行: 1725348564
三:使用YML配置文件配置定时任务参数
正常来说,我们在开发中是不希望过度的修改代码,那我们将这些参数配置到yml配置文件中,使用的时候直接从yml文件中取接可以了。
Yml配置文件内容:
# 定时任务参数
scheduling:
fixed-rate: 5000
fixed-delay: 10000
cron: 0 0 8 * * ?
我们将上方的代码改造一下:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyScheduledTasks
{
@Value("${scheduling.fixed-rate}")
private long fixedRate;
@Value("${scheduling.fixed-delay}")
private long fixedDelay;
/**
* 每隔5秒执行一次
*/
@Scheduled(fixedRateString = "${scheduling.fixed-rate}")
public void executeTaskWithFixedRate()
{
System.out.println("定时任务执行时间: " + System.currentTimeMillis() / 1000);
}
/**
* 上一个定时任务完成后10秒执行
*/
@Scheduled(fixedDelayString = "${scheduling.fixed-delay}")
public void executeTaskWithFixedDelay()
{
System.out.println("上一个定时任务结束之后延迟10秒执行: " + System.currentTimeMillis() / 1000);
}
/**
* 每天15点13分执行(就是使用cron定时任务的语法)
*/
@Scheduled(cron = "${scheduling.cron}")
public void executeTaskWithCronExpression() {
System.out.println("使用cron表达式执行的任务: " + System.currentTimeMillis() / 1000 );
}
}
这样写就方便多了,将常量都放到配置文件中,这样修改常量就可以了。
四:CRON定时任务语法
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。按顺序依次为:
1) 秒(0~59)
2) 分钟(0~59)
3) 小时(0~23)
4) 天(0~31)
5) 月(0~11)
6) 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7) 年份(1970-2099)
其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于”月份中的日期”和”星期中的日期”这两个元素互斥的,必须要对其中一个设置。
配置实例:
1) 每隔5秒执行一次:/5 * ?
2) 每隔1分钟执行一次:0 /1 ?
3) 0 0 10,14,16 ? 每天上午10点,下午2点,4点
4) 0 0/30 9-17 ? 朝九晚五工作时间内每半小时
5) 0 0 12 ? * WED 表示每个星期三中午12点
6) “0 0 12 ?” 每天中午12点触发
7) “0 15 10 ? “ 每天上午10:15触发
8) “0 15 10 ?” 每天上午10:15触发
9) “0 15 10 ? *” 每天上午10:15触发
10) “0 15 10 ? 2005” 2005年的每天上午10:15触发
11) “0 14 * ?” 在每天下午2点到下午2:59期间的每1分钟触发
12) “0 0/5 14 ?” 在每天下午2点到下午2:55期间的每5分钟触发
13) “0 0/5 14,18 ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
14) “0 0-5 14 ?” 在每天下午2点到下午2:05期间的每1分钟触发
15) “0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
16) “0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
17) “0 15 10 15 * ?” 每月15日上午10:15触发
18) “0 15 10 L * ?” 每月最后一日的上午10:15触发
19) “0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
20) “0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
21) “0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发
关键字符注意:
1) “?”字符仅被用于天(月)和天(星期)两个子表达式,表示不指定值 当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”
2) “L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写 如果在“L”前有具体的内容,它就具有其他的含义了。
3) W 字符代表着平日(Mon-Fri),并且仅能用于日域中。它用来指定离指定日的最近的一个平日。大部分的商业处理都是基于工作周的,所以 W 字符可能是非常重要的。
4) C:代表“Calendar”的意思。它的意思是计划所关联的日期,如果日期没有被关联,则相当于日历中所有日期。
以上大概就是springboot中定时任务注解@Scheduled的基本使用。
有好的建议,请在下方输入你的评论。