springboot下定时任务的使用
使用场景:需要定时的去获取配置表的数据。由于配置表的数据在程序运行时会更新,同时更新频率比较低,为了避免某些方法每一次都直接去获取,消耗性能,所以采用定时的方式主动去获取配置信息,提高高并发的吞吐量。
- 在对应的配置类添加注解@EnableScheduling,申明需要启动定时任务
- 在对应的方法添加@Scheduled,定义定时任务的方式
@EnableScheduling
@Configuration
public class SystemConfig {
private static Map<String, Object> resultMap = new HashMap<>();
@Scheduled(fixedDelayString = "${config.scheduled.fixedRate:60000}")
private void onFixDelay() {
resultMap = configService.getAllConfig();
}
}
Scheduled详解
- 支持cron表达式,可以通过类似“* * * 1 * ?”来表示每分钟执行,参考 http://www.bejson.com/othertools/cron/
- zone解决cron表示的时区问题
- fixedDelay,使用int来设置微妙为单位的定时任务
- fixedDelayString,使用String来设置n微妙为单位的定时任务,支持${}表达式,获取yaml、properties配置文件对应的属性值,作用在最后一次的调用与下一次调用开始的时间为n微妙(其中由于本身执行时间,可能间隔时间会长一些)
- initialDelay ,初次执行需要等待的时间,使用int来设置n微妙为单位
- initialDelayString ,初次执行需要等待的时间,使用String来设置n微妙为单位
- fixedRate,使用int来设置n微妙为单位的定时任务
- fixedRateString,使用String来设置微妙为单位的定时任务,支持${}表达式,获取yaml、properties配置文件对应的属性值,作用在每一次n微妙的时间间隔上
- fixedDelay与fixedRate的区别
@Scheduled(fixedRateString = "${config.scheduled.fixedRate:6000}")
private void onFixDelay() throws InterruptedException {
// 加点料,模拟某些复杂算法与io读取耗时,时间为4s
Thread.sleep(4000);
System.out.println(new Date());
resultMap = configService.getAllConfig();
}
fixedDelay,作用在最后一次的调用与下一次调用开始的时间为n微妙,包括方法本身的执行时间,可能间隔时间会长一些
fixedRate,作用在每一次n微妙的时间间隔上,不包括方法本身的执行时间
/**
* A cron-like expression, extending the usual UN*X definition to include
* triggers on the second as well as minute, hour, day of month, month
* and day of week. e.g. {@code "0 * * * * MON-FRI"} means once per minute on
* weekdays (at the top of the minute - the 0th second).
* @return an expression that can be parsed to a cron schedule
* @see org.springframework.scheduling.support.CronSequenceGenerator
*/
String cron() default "";
/**
* A time zone for which the cron expression will be resolved. By default, this
* attribute is the empty String (i.e. the server's local time zone will be used).
* @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)},
* or an empty String to indicate the server's default time zone
* @since 4.0
* @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone)
* @see java.util.TimeZone
*/
String zone() default "";
/**
* Execute the annotated method with a fixed period in milliseconds between the
* end of the last invocation and the start of the next.
* @return the delay in milliseconds
*/
long fixedDelay() default -1;
/**
* Execute the annotated method with a fixed period in milliseconds between the
* end of the last invocation and the start of the next.
* @return the delay in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String fixedDelayString() default "";
/**
* Execute the annotated method with a fixed period in milliseconds between
* invocations.
* @return the period in milliseconds
*/
long fixedRate() default -1;
/**
* Execute the annotated method with a fixed period in milliseconds between
* invocations.
* @return the period in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String fixedRateString() default "";
/**
* Number of milliseconds to delay before the first execution of a
* {@link #fixedRate()} or {@link #fixedDelay()} task.
* @return the initial delay in milliseconds
* @since 3.2
*/
long initialDelay() default -1;
/**
* Number of milliseconds to delay before the first execution of a
* {@link #fixedRate()} or {@link #fixedDelay()} task.
* @return the initial delay in milliseconds as a String value, e.g. a placeholder
* @since 3.2.2
*/
String initialDelayString() default "";