Spring Task定時(shí)任務(wù)的配置和使用詳解
記錄下Spring自帶的定時(shí)任務(wù)用法。
spring中使用定時(shí)任務(wù)
基于xml配置文件使用定時(shí)任務(wù)
首先配置spring開啟定時(shí)任務(wù)
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:annotation-driven /> <!-- 定時(shí)器開關(guān)--> <bean id="myTask" class="com.spring.task.MyTask"></bean> <task:scheduled-tasks> <!-- 這里表示的是每隔五秒執(zhí)行一次 --> <task:scheduled ref="myTask" method="show" cron="*/5 * * * * ?" /> <task:scheduled ref="myTask" method="print" cron="*/10 * * * * ?"/> </task:scheduled-tasks> <!-- 自動(dòng)掃描的包名 --> <context:component-scan base-package="com.spring.task" /> </beans>
定義自己的任務(wù)執(zhí)行邏輯
package com.spring.task; /** * 定義任務(wù) */ public class MyTask { public void show() { System.out.println("show method 1"); } public void print() { System.out.println("print method 1"); } }
基于注解使用定時(shí)任務(wù)
package com.spring.task; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * 基于注解的定時(shí)器 */ @Component public class MyTask2 { /** * 定時(shí)計(jì)算。每天凌晨 01:00 執(zhí)行一次 */ @Scheduled(cron = "0 0 1 * * *") public void show() { System.out.println("show method 2"); } /** * 啟動(dòng)時(shí)執(zhí)行一次,之后每隔2秒執(zhí)行一次 */ @Scheduled(fixedRate = 1000*2) public void print() { System.out.println("print method 2"); } }
這樣,當(dāng)項(xiàng)目啟動(dòng),定時(shí)任務(wù)就會(huì)按照規(guī)則按時(shí)執(zhí)行了。
Spring Boot中使用定時(shí)任務(wù)
Spring Boot中使用更加方便。
引入springboot starter
包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
在程序入口啟動(dòng)類添加@EnableScheduling
,開啟定時(shí)任務(wù)功能
@SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }
定義定時(shí)任務(wù)邏輯
@Component public class MyTask3 { private int count=0; @Scheduled(cron="*/6 * * * * ?") private void process() { System.out.println("this is scheduler task runing "+(count++)); } }
任務(wù)執(zhí)行規(guī)則說(shuō)明
先來(lái)看看@Scheduled
注解的源碼
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(Schedules.class) public @interface Scheduled { String cron() default ""; String zone() default ""; long fixedDelay() default -1; String fixedDelayString() default ""; long fixedRate() default -1; String fixedRateString() default ""; long initialDelay() default -1; String initialDelayString() default ""; }
可以看出,注解中可以傳8種參數(shù):
- cron:指定cron表達(dá)式
- zone:默認(rèn)使用服務(wù)器默認(rèn)時(shí)區(qū)??梢栽O(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有如下兩種語(yǔ)法格式:
- Seconds Minutes Hours DayofMonth Month DayofWeek Year
- Seconds Minutes Hours DayofMonth Month DayofWeek
每一個(gè)域可出現(xiàn)的字符如下:
- 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年
每一個(gè)域都使用數(shù)字,但還可以出現(xiàn)如下特殊字符,它們的含義是:
*
:表示匹配該域的任意值,假如在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ì)跨過(guò)月份。LW
:這兩個(gè)字符可以連用,表示在某個(gè)月最后一個(gè)工作日,即最后一個(gè)星期五。#
:用于確定每個(gè)月第幾個(gè)星期幾,只能出現(xiàn)在DayofMonth域。例如在4#2,表示某月的第二個(gè)星期三。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot使用TaskScheduler實(shí)現(xiàn)動(dòng)態(tài)增刪啟停定時(shí)任務(wù)方式
- springboot使用ThreadPoolTaskExecutor多線程批量插入百萬(wàn)級(jí)數(shù)據(jù)的實(shí)現(xiàn)方法
- springboot使用線程池(ThreadPoolTaskExecutor)示例
- 使用spring-task定時(shí)任務(wù)動(dòng)態(tài)配置修改執(zhí)行時(shí)間
- Spring Boot 使用WebAsyncTask異步返回結(jié)果
- 分布式調(diào)度器之Spring Task 的使用詳解
相關(guān)文章
Java的RxJava庫(kù)操作符的用法及實(shí)例講解
RxJava由于提供異步和基于事件的支持在Android開發(fā)者中獲得了不少人氣,這里我們就來(lái)看一下Java的RxJava庫(kù)操作符的用法及實(shí)例講解,需要的朋友可以參考下2016-06-06spring-data-redis 動(dòng)態(tài)切換數(shù)據(jù)源的方法
最近遇到了一個(gè)麻煩的需求,我們需要一個(gè)微服務(wù)應(yīng)用同時(shí)訪問(wèn)兩個(gè)不同的 Redis 集群,一般情況下我們會(huì)怎么處理呢,下面通過(guò)場(chǎng)景分析給大家介紹spring-data-redis 動(dòng)態(tài)切換數(shù)據(jù)源的方法,感興趣的朋友一起看看吧2021-08-08Java正則表達(dá)式處理特殊字符轉(zhuǎn)義的方法
由于正則表達(dá)式定了一些特殊字符,而有時(shí)候需要對(duì)這些特殊字符進(jìn)行匹配的話就需要進(jìn)行轉(zhuǎn)義了,下面這篇文章主要給大家介紹了Java正則表達(dá)式處理特殊字符轉(zhuǎn)義的方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-01-01