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 "";