亲宝软件园·资讯

展开

@Scheduled参数cron表达式

Qiwany 人气:31

@Scheduled参数及cron表达式解释

@Scheduled支持以下8个参数

cron表达式是一个字符串,以空格分开共6个域

如图:

在这里插入图片描述

上图类中有如下源代码:

private void parse(String expression) throws IllegalArgumentException {
	String[] fields = StringUtils.tokenizeToStringArray(expression, " ");
	if (!areValidCronFields(fields)) {
		throw new IllegalArgumentException(String.format(
				"Cron expression must consist of 6 fields (found %d in \"%s\")", fields.length, expression));
	}
	doParse(fields);
}
private static boolean areValidCronFields(String[] fields) {
	return (fields != null && fields.length == 6);
}

代码中expression参数即cron表达式,所以在spring自带的定时任务中正确的cron表达式只能是6个域,否则会抛出IllegalArgumentException提示:Cron expression must consist of 6 fields(cron表达式必须由6个域组成)。

必填 允许值 允许的通配符
秒(seconds) 0-59整数 , - * /
分(minutes) 0-59整数 , - * /
时(hours) 0-23整数 , - * /
日(daysOfMonth) 1-31整数(需要考虑月的天数) , - * ? / L W
月(months) 1-12整数 或 JAN-DEC , - * /
周 (daysOfWeek) 1-7整数 或 SUN-SAT , - * ? / L #

通配符说明

常用表达式示例

@Scheduled 定时任务总结

@Scheduled

@Scheduled 参数说明

生成器工具地址-http://cron.qqe2.com/

例子:@Scheduled(fixedDelay = 1000 * 10) //10秒发送一次

······················scheduled1开始执行·······················2018-07-27 14:00:00

······················scheduled1结束执行·······················2018-07-27 14:00:05

······················scheduled2开始执行·······················2018-07-27 14:00:15

······················scheduled2结束执行·······················2018-07-27 14:00:20

······················scheduled3开始执行·······················2018-07-27 14:00:30

······················scheduled3结束执行·······················2018-07-27 14:00:35

例子:@Scheduled(fixedRate = 1000 * 10) //10秒发送一次

······················scheduled1开始执行·······················2018-07-27 14:00:00

······················scheduled1结束执行·······················2018-07-27 14:00:05

······················scheduled2开始执行·······················2018-07-27 14:00:10

······················scheduled2结束执行·······················2018-07-27 14:00:15

······················scheduled3开始执行·······················2018-07-27 14:00:20

······················scheduled3结束执行·······················2018-07-27 14:00:25

注意事项

1.定时器的参数有两种写法是用cron表达式,或者使用fixedDelay、fixedRate等参数直接配置

需要注意的是 使用cron表达式的方法,在项目首次启动后不会直接运行,而是等到执行周期才会执行

而使用第二种方式的定时器方法,在项目启动成功后会马上开始执行一次,再按照时间周期执行。

测试说明:

使用第一种配置方式,项目启动后方法不会执行,而是等到执行周期到了才会执行方法

使用第二种参数方式的方法,项目启动成功后马上执行了一次

2.定时器默认为单线程,所以如果项目中使用多个定时器要配置线程池

注意这里的@EnableScheduling,使用它来开启定时器注解

@Configuration
@EnableScheduling
public class SchedulingConfig implements SchedulingConfigurer { 
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
		taskRegistrar.setScheduler(taskExecutor());
	}
 
	@Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
        return Executors.newScheduledThreadPool(5 ,new ThreadFactory() {
        	private final AtomicLong counter = new AtomicLong();
			
			@Override
			public Thread newThread(Runnable r) {
				Thread thread = new Thread(r);
				thread.setName("test-scheduler-" + counter.incrementAndGet());
				return thread;
			}			
		});
    }	
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论