使用定時(shí)器
- 開啟定時(shí)任務(wù)功能
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 配置定時(shí)任務(wù)
@Component
public class Task {
private int count=0;
@Scheduled(cron="*/6 * * * * ?")
private void process() {
System.out.println("this is scheduler task runing "+(count++));
}
}
任務(wù)執(zhí)行規(guī)則
@Scheduled注解:
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
/**
* 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 "";
}
- cron:指定cron表達(dá)式
- zone:默認(rèn)使用服務(wù)器默認(rèn)時(shí)區(qū)芥映∮匙可以設(shè)置為java.util.TimeZone中的zoneId
- fixedDelay:從上一個(gè)任務(wù)完成開始到下一個(gè)任務(wù)開始的間隔畅形,單位毫秒
- fixedDelayString:同上阅懦,時(shí)間值是String類型
- fixedRate:從上一個(gè)任務(wù)開始到下一個(gè)任務(wù)開始的間隔斜纪,單位毫秒
- fixedRateString:同上搔弄,時(shí)間值是String類型
- initialDelay:任務(wù)首次執(zhí)行延遲的時(shí)間幅虑,單位毫秒
- initialDelayString:同上,時(shí)間值是String類型
cron表達(dá)式
Cron表達(dá)式是一個(gè)字符串顾犹,字符串以5或6個(gè)空格隔開倒庵,分為6或7個(gè)域,每一個(gè)域代表一個(gè)含義炫刷,Cron有如下兩種語法格式:
- Seconds Minutes Hours DayofMonth Month DayofWeek Year
- Seconds Minutes Hours DayofMonth Month DayofWeek
可以填充的值:
- Seconds: 可出現(xiàn)
, - * /
四個(gè)字符擎宝,有效范圍為0-59的整數(shù) - Minutes: 可出現(xiàn)
, - * /
四個(gè)字符,有效范圍為0-59的整數(shù) - Hours: 可出現(xiàn)
, - * /
四個(gè)字符浑玛,有效范圍為0-23的整數(shù) - DayofMonth: 可出現(xiàn)
, - * / ? L W C
八個(gè)字符绍申,有效范圍為0-31的整數(shù) - Month: 可出現(xiàn)
, - * /
四個(gè)字符,有效范圍為1-12的整數(shù)或JAN-DEC - DayofWeek: 可出現(xiàn)
, - * / ? L C #
四個(gè)字符顾彰,有效范圍為1-7的整數(shù)或SUN-SAT兩個(gè)范圍极阅。1表示星期天,2表示星期一涨享, 依次類推 - Year: 可出現(xiàn)
, - * /
四個(gè)字符筋搏,有效范圍為1970-2099年
符號(hào)代表的意義:
-
*
:表示匹配該域的任意值,假如在Minutes域使用*, 即表示每分鐘都會(huì)觸發(fā)事件厕隧。 -
?
:只能用在DayofMonth和DayofWeek兩個(gè)域奔脐。它也匹配域的任意值俄周,但實(shí)際不會(huì)。因?yàn)镈ayofMonth和 DayofWeek會(huì)相互影響髓迎。例如想在每月的20日觸發(fā)調(diào)度峦朗,不管20日到底是星期幾,則只能使用如下寫法: 13 13 15 20 ?, 其中最后一位只能用竖般?甚垦,而不能使用,如果使用*表示不管星期幾都會(huì)觸發(fā)涣雕,實(shí)際上并不是這樣艰亮。 -
-
:表示范圍,例如在Minutes域使用5-20挣郭,表示從5分到20分鐘每分鐘觸發(fā)一次迄埃。 -
/
:表示起始時(shí)間開始觸發(fā),然后每隔固定時(shí)間觸發(fā)一次兑障,例如在Minutes域使用5/20,則意味著5分鐘觸發(fā)一次侄非,而25,45等分別觸發(fā)一次流译。
,:表示列出枚舉值值逞怨。例如:在Minutes域使用5,20,則意味著在5和20分每分鐘觸發(fā)一次福澡。 -
L
:表示最后叠赦,只能出現(xiàn)在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味著在最后的一個(gè)星期四觸發(fā)革砸。 -
W
:表示有效工作日(周一到周五),只能出現(xiàn)在DayofMonth域除秀,系統(tǒng)將在離指定日期的最近的有效工作日觸發(fā)事件。例如:在 DayofMonth使用5W算利,如果5日是星期六册踩,則將在最近的工作日:星期五,即4日觸發(fā)效拭。如果5日是星期天暂吉,則在6日(周一)觸發(fā);如果5日在星期一 到星期五中的一天缎患,則就在5日觸發(fā)借笙。另外一點(diǎn),W的最近尋找不會(huì)跨過月份较锡。 -
LW
:這兩個(gè)字符可以連用业稼,表示在某個(gè)月最后一個(gè)工作日,即最后一個(gè)星期五蚂蕴。 -
#
:用于確定每個(gè)月第幾個(gè)星期幾低散,只能出現(xiàn)在DayofMonth域俯邓。例如在4#2,表示某月的第二個(gè)星期三熔号。